Error Identifier: classConstant.missingNativeType
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 Base
{
public const string NAME = 'base';
}
class Child extends Base
{
public const NAME = 'child';
}
Why is it reported? #
A class constant overrides a parent constant that has a native type declaration (available since PHP 8.3), but the child constant does not declare a native type. When a parent constant has a native type, the overriding constant must also declare a compatible native type to maintain type safety.
In the example above, Base::NAME is typed as string, but Child::NAME does not declare a native type.
How to fix it #
Add the native type to the overriding constant:
<?php declare(strict_types = 1);
class Base
{
public const string NAME = 'base';
}
class Child extends Base
{
- public const NAME = 'child';
+ public const string NAME = 'child';
}
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.