Error Identifier: property.notWritable
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 string $name;
}
class ChildClass extends ParentClass
{
public string $name {
get => 'fixed';
}
}
Why is it reported? #
A child class overrides a writable property from a parent class but makes it non-writable. In the example above, ParentClass::$name is writable, but ChildClass::$name only defines a get hook without a set hook, making it read-only. This violates the property contract established by the parent class.
How to fix it #
Ensure the overriding property also supports writes:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
public string $name {
get => 'fixed';
+ set => $this->name = $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]