Error Identifier: property.parentPropertyFinal
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Base
{
final public string $name = 'base';
}
class Child extends Base
{
public string $name = 'child';
}
Why is it reported? #
The child class overrides a property that is declared as final in the parent class. Final properties cannot be overridden by child classes. This is a PHP language-level restriction enforced since PHP 8.4.
Properties declared with private(set) visibility are also implicitly final, because the set operation cannot be overridden.
How to fix it #
Remove the overriding property declaration from the child class:
class Child extends Base
{
- public string $name = 'child';
}
If the child class needs different behavior, ask the parent class maintainer to remove the final keyword, or use a different property name:
class Child extends Base
{
- public string $name = 'child';
+ public string $displayName = 'child';
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Properties\OverridingPropertyRule [1]