[RFC] [Discussion] `#[\Override]` for class constants

php.internals

Daniel Scherzer

154 days ago
Hi internals, I'd like to start the discussion for a new RFC about adding support for `#[\Override]` for class constants. * RFC: https://wiki.php.net/rfc/override_constants * Implementation: https://github.com/php/php-src/pull/20478 I'd also like to draw specific attention to the open question, which I am soliciting feedback on: how should `#[\Override]` work for enum cases? See the RFC page for details. Thanks, -Daniel

Rowan Tommins [IMSoP]

154 days ago
On 31 March 2026 00:36:42 BST, Daniel Scherzer <daniel.e.scherzer@gmail.com> wrote:
>Hi internals, > >I'd like to start the discussion for a new RFC about adding support for >`#[\Override]` for class constants. > >* RFC: https://wiki.php.net/rfc/override_constants >* Implementation: https://github.com/php/php-src/pull/20478
I will probably abstain on this, because I've never fully understood the rationale for this attribute, but one point that might be worth spelling out is that by nature constants are static, which makes it slightly harder to accidentally reference an overridden definition. "Foo::BAR" and "self::BAR" are both guaranteed to reference the expected definition; only less common forms like "static::BAR" and "$this::BAR" will look up on a sub-class. Rowan Tommins [IMSoP]

Derick Rethans

154 days ago
On Mon, 30 Mar 2026, Daniel Scherzer wrote:
> I'd like to start the discussion for a new RFC about adding support for > `#[\Override]` for class constants. > > * RFC: https://wiki.php.net/rfc/override_constants > * Implementation: https://github.com/php/php-src/pull/20478 > > I'd also like to draw specific attention to the open question, which I am > soliciting feedback on: how should `#[\Override]` work for enum cases? See > the RFC page for details.
I think I'm on the " Always reject using #[\Override] on an enum case, saying that the attribute does not target enum cases" side here, as mixing class constants with enum cases sounds like a mismatch to me. cheers, Derick

Daniel Scherzer

138 days ago
On Tue, Mar 31, 2026 at 2:56 AM Derick Rethans <derick@php.net> wrote:
> > I think I'm on the " Always reject using #[\Override] on an enum case, > saying that the attribute does not target enum cases" side here, as > mixing class constants with enum cases sounds like a mismatch to me. > > cheers, > Derick >
On the other hand, if someone is intentionally overriding an interface constant with an enum case, all the more reason that the overriding should be explicitly documented, which is what `#[\Override]` would do. -Daniel

Gina P. Banyard

154 days ago
On Tuesday, 31 March 2026 at 00:45, Daniel Scherzer <daniel.e.scherzer@gmail.com> wrote: > Hi internals, > > I'd like to start the discussion for a new RFC about adding support for `#[\Override]` for class constants. > > * RFC: https://wiki.php.net/rfc/override_constants > * Implementation: https://github.com/php/php-src/pull/20478 > > I'd also like to draw specific attention to the open question, which I am soliciting feedback on: how should `#[\Override]` work for enum cases? See the RFC page for details. It's impossible to override an enum case. Enums are final and cannot be extended. So just error if someone tries to use `#[\Override]` on an enum case as it is non-sensical. Best regards, Gina P. Banyard

Tim Düsterhus

152 days ago
Hi Am 2026-03-31 01:36, schrieb Daniel Scherzer:
> I'd also like to draw specific attention to the open question, which I > am > soliciting feedback on: how should `#[\Override]` work for enum cases? > See > the RFC page for details.
We've been pretty clear so far the “enum cases are just constants”, e.g. in the RFC for `#[\Deprecated]` (https://wiki.php.net/rfc/deprecated_attribute):
> Enum cases are internally implemented as class constants. Adding the > #[\Deprecated] attribute to an enum case will behave as expected.
Given that: interface NamedX { public const string Name = 'foo'; } enum Foo implements NamedX { case Name; } is a thing already and fails with: Type of Foo::Name must be compatible with Named::Name of type string as expected and interface NamedX { public const Name = 'foo'; } enum Foo implements NamedX { case Name; } without defining the constant's type just works by the enum case shadowing the interface’s constant it seems consequent to allow `#[\Override]` on enum cases, even if likely not particularly useful. Best regards Tim Düsterhus

Daniel Scherzer

135 days ago
Hi internals, I have updated the RFC to resolve the open issue about `#[\Override]` on enum cases - when marked with `#[\Override]`, the enum case must be overriding an inherited class constant. The fact that this is so uncommon makes it all the more important that when it is intentional, it can be made clear. This qualifies as a "major change" to the RFC and triggers a 14 day cooldown period. -Daniel On Mon, Mar 30, 2026 at 4:36 PM Daniel Scherzer <daniel.e.scherzer@gmail.com> wrote:

Daniel Scherzer

120 days ago
Hi internals, It has been more than 14 days, so the cooldown period is over. Having received no further feedback, this is your official intent to vote message - I plan to open the vote in the next few days if there are no new concerns. -Daniel On Sat, Apr 18, 2026 at 6:19 PM Daniel Scherzer <daniel.e.scherzer@gmail.com> wrote: