Error Identifier: staticClassAccess.privateProperty
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 Counter
{
private static int $count = 0;
public function increment(): void
{
static::$count++;
}
}
Why is it reported? #
Accessing a private static property through static:: instead of self:: is unsafe. The static:: keyword uses late static binding, which resolves to the class that actually calls the method at runtime. If a child class calls this method, static:: would resolve to the child class, but private properties are not inherited and belong only to the declaring class. This can lead to unexpected behavior.
How to fix it #
Use self:: instead of static:: to access private static properties, since they belong exclusively to the declaring class.
<?php declare(strict_types = 1);
class Counter
{
private static int $count = 0;
public function increment(): void
{
- static::$count++;
+ self::$count++;
}
}
How to ignore this error #
You can use the identifier staticClassAccess.privateProperty to ignore this error using a comment:
// @phpstan-ignore staticClassAccess.privateProperty
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: staticClassAccess.privateProperty
Rules that report this error #
- PHPStan\Rules\Properties\AccessPrivatePropertyThroughStaticRule [1]