Menu

← Back to classConstant.*

Error Identifier: classConstant.protected

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
{
    protected const BAR = 1;
}

echo Foo::BAR;

Why is it reported? #

The code accesses a protected class constant from a scope that does not have access to it. Protected constants can only be accessed from within the declaring class or its subclasses. Accessing them from outside the class hierarchy causes a fatal error at runtime.

How to fix it #

Access the constant from within the class hierarchy:

 <?php declare(strict_types = 1);
 
 class Foo
 {
     protected const BAR = 1;

+    public function getBar(): int
+    {
+        return self::BAR;
+    }
 }

-echo Foo::BAR;
+echo (new Foo())->getBar();

Or change the constant’s visibility to public if it should be accessible from outside:

 <?php declare(strict_types = 1);
 
 class Foo
 {
-    protected const BAR = 1;
+    public const BAR = 1;
 }

 echo Foo::BAR;

How to ignore this error #

You can use the identifier classConstant.protected to ignore this error using a comment:

// @phpstan-ignore classConstant.protected
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.protected

Rules that report this error #

  • PHPStan\Rules\Classes\ClassConstantRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.