Error Identifier: property.visibility
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 Base
{
public string $name = 'hello';
}
class Child extends Base
{
protected string $name = 'world'; // ERROR: Protected property Child::$name overriding public property Base::$name should also be public.
}
Why is it reported? #
When a child class overrides a property from a parent class, it must not reduce the visibility of the property. A public property cannot become protected or private, and a protected property cannot become private. Reducing visibility violates the Liskov Substitution Principle – code that accesses the property through the parent type expects the declared visibility level to be maintained.
How to fix it #
Match or widen the visibility of the overriding property:
<?php declare(strict_types = 1);
class Base
{
public string $name = 'hello';
}
class Child extends Base
{
- protected string $name = 'world';
+ public string $name = 'world';
}
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.