Menu

← Back to method.*

Error Identifier: method.dynamicName

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);

function doFoo(object $obj): void
{
	$method = 'doSomething';
	$obj->$method(); // ERROR: Variable method call on object.
}

Why is it reported? #

This error is reported by phpstan/phpstan-strict-rules.

A method is being called using a variable or expression as the method name instead of a static identifier. Variable method calls make the code harder to analyse statically, obscure which method is actually being called, and bypass IDE refactoring tools. PHPStan cannot verify that the method exists, has the correct signature, or is callable in this context.

How to fix it #

Replace the variable method call with a direct method call:

 <?php declare(strict_types = 1);
 
 function doFoo(object $obj): void
 {
-	$method = 'doSomething';
-	$obj->$method();
+	$obj->doSomething();
 }

If the method name must be dynamic, consider using a match expression or a map of known method names to callables:

 <?php declare(strict_types = 1);
 
 function doFoo(object $obj, string $action): void
 {
-	$obj->$action();
+	match ($action) {
+		'start' => $obj->start(),
+		'stop' => $obj->stop(),
+		default => throw new \InvalidArgumentException('Unknown action'),
+	};
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\VariableVariables\VariableMethodCallRule [1] phpstan/phpstan-strict-rules
  • PHPStan\Rules\VariableVariables\VariableMethodCallableRule [1] phpstan/phpstan-strict-rules

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.