Method name must be a string

Description

When using a dynamic syntax to call a method, the name of the method must be a string. It cannot be any other type, as no type juggling will take place. In particular, objects with toString are not converted to their string value.

Although method names have a specific format, this is not checked here, and the string is used raw.

Note that there is not equivalent for dynamic property names: they are cast to string as needed.

Example

<?php

class X {
    static function foo() {}
}

$b = 1;
X::$b();

?>

Solutions

  • Cast the variable to string first, with (string).

  • Check if the variable is a string before using it.

  • Use a switch to hardcode the method name.