Skip to content
PHP News
Search
Implemented PHP 8.6

#[\Override] for class constants

Lets the #[\Override] attribute go on class constants and enum cases, so PHP errors if nothing is actually being overridden.

Extend #[\Override] to target class constants?

Primary vote · 2/3 majority

15 Yes 0 No 4 abstain 100% approval

This poll has closed.

Accepted. Voting closed on May 22, 2026 with 15 in favor, 0 against and 4 abstaining, clearing the two-thirds majority it needed. It's in PHP 8.6.

Summary

The #[\Override] attribute tells PHP that a member is meant to replace something from a parent, and PHP throws an error if nothing is actually being replaced. It arrived in PHP 8.3 for methods, and PHP 8.5 extended it to properties. This RFC from Daniel Scherzer lets you use it on class constants too.

A child class can redefine a constant from its parent, but when you read the code, you can't always tell whether that was intentional. The author may have just picked the same name for a new, unrelated constant. The attribute makes the intent explicit, and PHP verifies it.

How it works

class Demo {
    #[\Override] // this triggers an error
    public const C = 'C';
}

class Base {
    protected const C = 'C';
}

class Child extends Base {
    #[\Override] // no error, override is validated
    public const C = 'Changed';
}

The rules match the ones for methods and properties:

  • A public or protected constant in a parent class or interface counts as something to override. A private one doesn't.
  • On traits, the attribute is checked in the class that uses the trait.
  • It works on anonymous classes and on interfaces.
  • On enums, both constants and enum cases can use it, but they must override a constant from an interface the enum implements.

An earlier version of the RFC left open whether enum cases should be allowed at all. After discussion on the mailing list, the author decided to treat them like any other class constant.

What it means for existing code

Nothing breaks. Before this change, #[\Override] on a constant was always an error because it wasn't supported. Now it's only an error when nothing is overridden, and that holds even with #[\DelayedTargetValidation] on PHP 8.6 and later. IDEs and static analyzers will likely add rules for it.