PHP RFC: Extend #[\Override] to target class constants
- Version: 0.1
- Date: 2026-03-13
- Author: Daniel Scherzer, daniel.e.scherzer@gmail.com
- Status: Draft
- Implementation: https://github.com/php/php-src/pull/20478
Introduction
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.
Proposal
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):
- Public and protected constants of a parent class or implemented interface satisfy
#[\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.
Examples
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'; } ?>
Backward Incompatible Changes
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.
Proposed PHP Version(s)
Next minor version (PHP 8.6).
RFC Impact
To the Ecosystem
IDEs and static analyzers will likely want to extend their rules for #[\Override] to constants.
To Existing Extensions
Extensions might want to add the attribute to their constants where appropriate.
To SAPIs
None
Open Issues
None
Voting Choices
Extend #[\Override] to target class constants?
Primary Vote requiring a 2/3 majority to accept the RFC:
Patches and Tests
Implementation
After the RFC is implemented, this section should contain:
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
References
Rejected Features
Keep this updated with features that were discussed on the mail lists.
Changelog
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.