Menu

← Back to classConstant.*

Error Identifier: classConstant.nameNotString

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 evaluate to a string. If the expression has a non-string type such as int, object, or mixed, PHP will throw a TypeError at runtime. In the example above, $name is an int, which is not a valid class constant name.

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]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.