%s::$%s has #[\Override] attribute

Description

PHP 8.5 extends the #[\Override] attribute, previously usable only on methods, so that it can also be applied to properties. It tells the engine that the property is expected to override a property of the same name declared in a parent class or an implemented interface, so that the engine can check that this is really the case.

Here, Y::$b is marked as overriding, but X (the parent of Y) has no $b property at all – only $a. Since there is nothing to override, the attribute is meaningless, and PHP reports it as an error at compile time. The full message continues with ‘, but no matching parent property exists’.

This error is also raised when the class using the attribute has no parent at all, or when the property is declared in a trait whose using class has no matching parent property.

Example

<?php

class X {
    public int $a = 1;
}

class Y extends X {
    #[\Override]
    public int $b = 2;
}

?>

Literal Examples

  • Y::$b has #[Override] attribute, but no matching parent property exists

Solutions

  • Remove the #[\Override] attribute.

  • Rename the property to match the name used by the parent class or interface.

  • Add the property, with the same name, to the parent class.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See New in PHP 8.5: the #[Override] attribute could previously only target methods; PHP 8.5 allows it on properties too..