Error Identifier: classConstant.visibility
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 BAR = 1;
}
class Bar extends Foo
{
private const BAR = 2;
}
Why is it reported? #
A class constant overrides a parent class or trait constant with a more restrictive visibility. PHP requires that an overriding constant must have the same or less restrictive visibility than the constant it overrides. For example, a public constant cannot be overridden as protected or private, and a protected constant cannot be overridden as private. Violating this rule causes a fatal error at runtime.
How to fix it #
Match or widen the visibility to be compatible with the parent:
<?php declare(strict_types = 1);
class Foo
{
public const BAR = 1;
}
class Bar extends Foo
{
- private const BAR = 2;
+ public const BAR = 2;
}
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.