Error Identifier: property.hookReadOnly
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 readonly int $value { // ERROR: Hooked properties cannot be readonly.
get => 42;
}
}
Why is it reported? #
PHP does not allow combining property hooks with the readonly modifier. This is a language-level restriction since PHP 8.4 introduced property hooks. The readonly modifier and property hooks have conflicting semantics – readonly restricts when a property can be written, while hooks allow defining custom get/set behavior that could conflict with the readonly guarantee.
How to fix it #
Remove the readonly modifier if the property needs hooks:
<?php declare(strict_types = 1);
class Foo
{
- public readonly int $value {
+ public int $value {
get => 42;
}
}
Alternatively, if the property should be readonly, remove the hooks:
<?php declare(strict_types = 1);
class Foo
{
- public readonly int $value {
- get => 42;
- }
+ public readonly int $value;
}
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]