Menu

← Back to property.*

Error Identifier: property.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 string $bar = 'hello';
}

function doFoo(Foo $foo): void
{
	$name = 123;
	echo $foo->$name;
}

Why is it reported? #

When accessing an object property dynamically using the $obj->$name syntax, the $name expression must evaluate to a string. If the value used as the property name is not a string and cannot be converted to a string (for example, an integer, array, or object without __toString()), the property access is invalid and will produce unexpected behavior or errors at runtime.

In the example above, the variable $name is an integer (123), which is not a valid property name.

How to fix it #

Ensure the dynamic property name is a string:

 <?php declare(strict_types = 1);
 
 function doFoo(Foo $foo): void
 {
-	$name = 123;
+	$name = 'bar';
 	echo $foo->$name;
 }

Or use a direct property access if the property name is known:

 <?php declare(strict_types = 1);
 
 function doFoo(Foo $foo): void
 {
-	$name = 123;
-	echo $foo->$name;
+	echo $foo->bar;
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Properties\AccessPropertiesInAssignRule [1]
  • PHPStan\Rules\Properties\AccessPropertiesRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.