Error Identifier: classConstant.value
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
{
public const int VALUE = 'hello';
}
Why is it reported? #
The value assigned to a class constant does not match the constant’s declared type. PHP 8.3 introduced native types for class constants. When a type is declared, the assigned value must be compatible with that type.
This error is also reported when a class overrides a trait constant with a different value, which can lead to inconsistencies.
How to fix it #
Change the value to match the declared type:
class Foo
{
- public const int VALUE = 'hello';
+ public const int VALUE = 42;
}
Or change the type to match the value:
class Foo
{
- public const int VALUE = 'hello';
+ public const string VALUE = 'hello';
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.