In PHP 8.3, the #[\Override] attribute was added (marking_overriden_methods). In PHP 8.5, it was extended to allow use on properties (override_properties). This RFC proposes to further expand the attribute to target class constants as well.
Class constants can override constants defined in parent classes, which can create a footgun. While it has been possible to mark a class constant as final since PHP 8.1 (final_class_const), when a class constant is overridden it is not always clear if the intention is to override a class constant in the parent class, or to define a new (unrelated) constant.
The #[\Override] attribute has already solved this problem by making it explicit (for methods and properties) that an override is intentional, and triggers an error when no override is being performed. This RFC expands the same functionality to class constants.
Allow the #[\Override] attribute to be used on class constants, with the same validation as methods and properties (modulo the fact that there are no abstract constants the way there are properties and methods):
#[\Override].. Private constants do not satisfy the attribute.#[\Override] is ignored on traits, but constants from a used trait behave as if the property definition was copied and pasted into the target class. Specifically the #[\Override] attribute on a trait constant requires the existence of a matching constant in a parent class or implemented interface.#[\Override] works as expected on anonymous classes.#[\Override] works as expected on interfaces: a matching constant needs to exist in a parent interface.#[\Override] works as expected on enums.Simple example:
<?php class Demo { #[\Override] // this triggers an error public const C = 'C'; } ?>
<?php class Base { protected const C = 'C'; } class Child extends Base { #[\Override] // no error, override is validated public const C = 'Changed'; } ?>
There should be no backwards incompatibilities from this change - the #[\Override] attribute previously always caused errors on class constants (because it was not supported), now it will only sometimes cause errors (when nothing is being overridden).
Note that for interaction with #[\DelayedTargetValidation], class constants work the same as properties and methods - even if #[\DelayedTargetValidation] is present, on PHP 8.6+ the #[\Override] attribute on class constants will still result in an error if the class constant is not overriding anything.
Next minor version (PHP 8.6).
IDEs and static analyzers will likely want to extend their rules for #[\Override] to constants.
Extensions might want to add the attribute to their constants where appropriate.
None
None
Extend #[\Override] to target class constants?
Primary Vote requiring a 2/3 majority to accept the RFC:
After the RFC is implemented, this section should contain:
Keep this updated with features that were discussed on the mail lists.
If there are major changes to the initial proposal, please include a short summary with a date or a link to the mailing list announcement here, as not everyone has access to the wikis' version history.