Table of Contents

PHP RFC: Extend #[\Override] to target class constants

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):

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:

Extend #[\Override] to target class constants?
Real name Yes No Abstain
Final result: 0 0 0
This poll has been closed.

Patches and Tests

https://github.com/php/php-src/pull/20478

Implementation

After the RFC is implemented, this section should contain:

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

* override_properties * marking_overriden_methods * final_class_const

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.