Error Identifier: property.finalPrivateHook
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 Foo
{
private string $name {
final get => $this->name;
}
}
Why is it reported? #
PHP does not allow a private property to have a final hook. The final modifier on a hook prevents child classes from overriding that hook, but private properties and their hooks are already invisible to child classes and cannot be overridden. Combining private visibility with a final hook is contradictory and results in a compile-time error in PHP 8.4+.
How to fix it #
Remove the final modifier from the hook, since private property hooks cannot be overridden anyway:
<?php declare(strict_types = 1);
class Foo
{
private string $name {
- final get => $this->name;
+ get => $this->name;
}
}
If the hook needs to be final to prevent overriding, change the property visibility to protected or public:
<?php declare(strict_types = 1);
class Foo
{
- private string $name {
+ protected string $name {
final get => $this->name;
}
}
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\PropertyInClassRule [1]