Menu

Error Identifier: classConstant.nameNotString

← Back to classConstant.*

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 = 'bar';
}

function doFoo(int $name): void
{
	echo Foo::{$name};
}

Why is it reported? #

When accessing a class constant dynamically with ClassName::{$expr} (available since PHP 8.3), the expression must be a string. Constant names are not cast to string at runtime, so even an object implementing Stringable is not accepted — only an actual string works. If the expression has a non-string type such as int, object, or mixed, the access is invalid. In the example above, $name is an int, which is not a valid class constant name.

This check is currently part of Bleeding Edge.

How to fix it #

Ensure the dynamic constant name expression is a string:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	public const BAR = 'bar';
 }

-function doFoo(int $name): void
+function doFoo(string $name): void
 {
 	echo Foo::{$name};
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Classes\ClassConstantRule [1]
  • PHPStan\Rules\Methods\CallMethodsRule [1]
  • PHPStan\Rules\Methods\CallStaticMethodsRule [1]
  • PHPStan\Rules\Properties\AccessPropertiesInAssignRule [1]
  • PHPStan\Rules\Properties\AccessPropertiesRule [1]
  • PHPStan\Rules\Properties\AccessStaticPropertiesInAssignRule [1]
  • PHPStan\Rules\Properties\AccessStaticPropertiesRule [1]
  • PHPStan\Rules\Variables\DefinedVariableRule [1]
Theme
A
© 2026 PHPStan s.r.o.