Menu
Error Identifier: staticProperty.protected
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
{
protected static int $counter = 0;
}
class Unrelated
{
public function read(): int
{
return Base::$counter;
}
}
Why is it reported? #
The code accesses a protected static property from a class that is not a subclass of the declaring class. Protected properties can only be accessed from within the declaring class or its descendants.
How to fix it #
Access the property from within the class hierarchy, or provide a public accessor method.
<?php declare(strict_types = 1);
class Base
{
protected static int $counter = 0;
+
+ public static function getCounter(): int
+ {
+ return static::$counter;
+ }
}
class Unrelated
{
public function read(): int
{
- return Base::$counter;
+ return Base::getCounter();
}
}
How to ignore this error #
You can use the identifier staticProperty.protected to ignore this error using a comment:
// @phpstan-ignore staticProperty.protected
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: staticProperty.protected