website/errors/classConstant.private.md
<?php declare(strict_types = 1);
class Foo
{
private const SECRET = 'hidden';
}
echo Foo::SECRET;
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.
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();