Override Attribute Extended To Class Constants And Enum Cases
The #[\Override] attribute, introduced in PHP 8.3 for methods and extended to properties in PHP 8.5, is further extended in PHP 8.6 to class constants and enum cases (an enum case being internally a special kind of class constant). It tells the engine that the constant is expected to override a constant of the same name declared in a parent class or an implemented interface, so the engine can check that this is really the case.
If the constant marked #[\Override] has no matching parent constant – because the parent/interface declares no constant of that name, or because the class has no parent and implements no interface at all – PHP reports a compile-time error. Only public and protected constants of a parent class or implemented interface satisfy the attribute; private constants do not count.
PHP code
<?php
interface Shape {
const SIDES = 0;
}
class Square implements Shape {
#[\Override]
const SIDES = 4; // Fine, overrides Shape::SIDES
#[\Override]
const COLOR = 'blue'; // Fatal error, no matching parent constant
}
?>
Before
PHP Fatal error: Attribute "Override" cannot target class constant (allowed targets: method)
Fatal error: Attribute "Override" cannot target class constant (allowed targets: method)
After
PHP Fatal error: Square::COLOR has #[\Override] attribute, but no matching parent constant exists
PHP version change
This behavior changed in 8.6.