Error Identifier: outOfClass.static
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);
$value = static::FOO;
Why is it reported? #
The static keyword is being used to access a class constant (or in a class-name context) outside of a class scope. The static keyword in this context refers to the class the method was called on (late static binding), but it is only meaningful inside a class method. Using it outside of a class results in a fatal error at runtime.
How to fix it #
Use the actual class name instead of static, or move the code inside a class method.
<?php declare(strict_types = 1);
-$value = static::FOO;
+$value = MyClass::FOO;
Or move the code inside a class:
<?php declare(strict_types = 1);
class MyClass
{
public const FOO = 'bar';
public function getValue(): string
{
return static::FOO;
}
}
How to ignore this error #
You can use the identifier outOfClass.static to ignore this error using a comment:
// @phpstan-ignore outOfClass.static
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: outOfClass.static
Rules that report this error #
- PHPStan\Rules\Classes\ClassConstantRule [1]
- PHPStan\Rules\Classes\ExistingClassInInstanceOfRule [1]
- PHPStan\Rules\Classes\InstantiationRule [1]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1]
- PHPStan\Rules\Methods\StaticMethodCallableRule [1]
- PHPStan\Rules\Properties\AccessStaticPropertiesInAssignRule [1]
- PHPStan\Rules\Properties\AccessStaticPropertiesRule [1]