Error Identifier: classConstant.nonObject
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);
function foo(int $value): void
{
echo $value::FOO;
}
Why is it reported? #
The code attempts to access a class constant using the :: operator on a value that is not an object or a class string. Class constants can only be accessed on objects, class names, or class-string types.
In the example above, $value is an int, which does not support class constant access.
How to fix it #
Ensure the value used for class constant access is an object or a class-string:
<?php declare(strict_types = 1);
-function foo(int $value): void
+function foo(Foo $value): void
{
echo $value::FOO;
}
How to ignore this error #
You can use the identifier classConstant.nonObject to ignore this error using a comment:
// @phpstan-ignore classConstant.nonObject
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: classConstant.nonObject
Rules that report this error #
- PHPStan\Rules\Classes\ClassConstantRule [1]