Skip to content

#[Override] on properties leads to unexpected backwards incompatible changes #20878

@kkmuffme

Description

@kkmuffme

Description

The following code:

<?php

class A {
    public string $foo;
}

class B extends A {
    #[Override]
    public string $foo;
}

https://3v4l.org/om5mJ#v8.5.1
=> no error
https://3v4l.org/om5mJ#v8.3.29

Fatal error: Attribute "Override" cannot target property (allowed targets: method)
https://3v4l.org/om5mJ#v8.2.30
=> no error

This creates the weird case where code works fine in older PHP and in the latest PHP but you get a fatal for some versions in between, which means the attribute cannot be used for properties at all for quite some time when you need to support all maintained PHP versions (it's only safe to use from 2029 when PHP 8.4 support ends)

Possibly for properties we change it to #[OverrideProperty] (or whatever) to prevent this issue? (and deprecate Override on properties again with 8.6, the OverrideProperty attribute could ship with the next 8.5 version though)

The RFC https://wiki.php.net/rfc/override_properties says no backward incompatible changes, however this is quite incompatible, isn't it?

PHP Version

8.5
8.3/8.4
<8.3

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions