Menu
Error Identifier: classConstant.private
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 const SECRET = 'hidden';
}
echo Foo::SECRET;
Why is it reported? #
The code is accessing a class constant that has private visibility from outside the class where it is declared. Private constants can only be accessed from within the same class.
How to fix it #
If you need to access the value from outside the class, change the constant’s visibility:
<?php declare(strict_types = 1);
class Foo
{
- private const SECRET = 'hidden';
+ public const SECRET = 'hidden';
}
echo Foo::SECRET;
Or provide a public method to access the value:
<?php declare(strict_types = 1);
class Foo
{
private const SECRET = 'hidden';
+ public static function getSecret(): string
+ {
+ return self::SECRET;
+ }
}
-echo Foo::SECRET;
+echo Foo::getSecret();
How to ignore this error #
You can use the identifier classConstant.private to ignore this error using a comment:
// @phpstan-ignore classConstant.private
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.private
Rules that report this error #
- PHPStan\Rules\Classes\ClassConstantRule [1]