Menu

Error Identifier: method.nameNotString

← Back to method.*

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 function doFoo(): void
	{
	}

	public function test(int $name): void
	{
		$this->$name();
	}
}

Why is it reported? #

When calling a method dynamically with the $obj->{$name}() syntax, the $name expression must be a string. Method names are not cast to string at runtime, so even an object implementing Stringable is not accepted — only an actual string works. In the example above, $name is an int, which cannot be used as a method name and results in an error.

This check is currently part of Bleeding Edge.

How to fix it #

Ensure the dynamic method name expression is a string:

-	public function test(int $name): void
+	public function test(string $name): void
 	{
 		$this->$name();
 	}

If the value comes from a Stringable object, cast it to string first:

 	public function test(\Stringable $name): void
 	{
-		$this->$name();
+		$this->{(string) $name}();
 	}

How to ignore this error #

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

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