Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Override Attribute Extended To Properties

The #[\Override] attribute, introduced in PHP 8.3, could originally only target methods. PHP 8.5 extends it so it can also be applied to properties, telling 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 the engine can check that this is really the case.

If the property marked #[\Override] has no matching parent property – because the parent has no property of that name, because the class has no parent at all, or because the property comes from a trait whose using class has no matching parent property – PHP reports a compile-time error.

PHP code

<?php

class X {
    public int $a = 1;
}

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

?>

Before

PHP Fatal error:  Attribute "Override" cannot target property (allowed targets: method)

After

PHP Fatal error:  Y::$b has #[\Override] attribute, but no matching parent property exists

PHP version change

This behavior changed in 8.5.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat