Error Identifier: staticProperty.private
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
{
private static int $foo = 1;
}
class ChildClass extends ParentClass
{
public function doFoo(): int
{
return static::$foo;
}
}
Why is it reported? #
The code accesses a private static property of a parent class from a child class. Private properties are only visible within the class that declares them and cannot be accessed from subclasses. This results in a fatal error at runtime.
How to fix it #
Change the property visibility to protected so that child classes can access it:
<?php declare(strict_types = 1);
class ParentClass
{
- private static int $foo = 1;
+ protected static int $foo = 1;
}
Alternatively, provide a protected or public accessor method in the parent class:
<?php declare(strict_types = 1);
class ParentClass
{
private static int $foo = 1;
+
+ protected static function getFoo(): int
+ {
+ return self::$foo;
+ }
}
class ChildClass extends ParentClass
{
public function doFoo(): int
{
- return static::$foo;
+ return static::getFoo();
}
}
How to ignore this error #
You can use the identifier staticProperty.private to ignore this error using a comment:
// @phpstan-ignore staticProperty.private
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.private