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 ParentClass
{
public int $prop = 1;
}
class ChildClass extends ParentClass
{
private int $storage = 1;
public int $prop {
&get => $this->storage;
set {
$this->storage = $value;
}
}
}
Why is it reported? #
A backed property (a property with its own storage, here inherited from the parent class) has both a set hook and a get hook that returns by reference. A reference returned from &get would allow writing to the property without going through the set hook, bypassing its logic. PHP does not allow this combination and reports a fatal error when the class is declared.
How to fix it #
Return by value from the get hook:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
private int $storage = 1;
public int $prop {
- &get => $this->storage;
+ get => $this->storage;
set {
$this->storage = $value;
}
}
}
Or remove the set hook if the property should be modified through the returned reference:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
private int $storage = 1;
public int $prop {
&get => $this->storage;
- set {
- $this->storage = $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\OverridingPropertyRule [1]