Error Identifier: property.hookedStatic
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
{
public static string $name { // ERROR: Hooked properties cannot be static.
get => 'hello';
}
}
Why is it reported? #
PHP does not allow property hooks on static properties. Property hooks (get and set) are designed to work with instance properties and rely on $this context. Static properties belong to the class rather than to an instance, so hooks are not supported for them.
How to fix it #
If the property needs hooks, make it an instance property:
<?php declare(strict_types = 1);
class Foo
{
- public static string $name {
+ public string $name {
get => 'hello';
}
}
If the property needs to be static, remove the hooks and use a static method instead:
<?php declare(strict_types = 1);
class Foo
{
- public static string $name {
- get => 'hello';
- }
+ public static string $name = 'hello';
}
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.