Error Identifier: staticClassAccess.privateMethod
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 Foo
{
private static function secret(): void
{
}
public function doFoo(): void
{
static::secret();
}
}
Why is it reported? #
A private method is called through static:: in a non-final class. The static:: keyword resolves at runtime to the actual class, which may be a child class. Since private methods are not inherited by child classes, calling a private method through static:: can cause a fatal error if the method is invoked from a child class context.
How to fix it #
Use self:: instead of static:: for private method calls:
<?php declare(strict_types = 1);
class Foo
{
private static function secret(): void
{
}
public function doFoo(): void
{
- static::secret();
+ self::secret();
}
}
Or make the class final, or make the method protected or public.
How to ignore this error #
You can use the identifier staticClassAccess.privateMethod to ignore this error using a comment:
// @phpstan-ignore staticClassAccess.privateMethod
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.privateMethod
Rules that report this error #
- PHPStan\Rules\Methods\CallPrivateMethodThroughStaticRule [1]