Calling a method with an array value in PHP
I have a class like this
class someclass{
public function somemethod(){}
}
Now I have an array:
$somearray['someclass'] = new someclass();
$somearray['somemethod'] = 'somemethod';
How can I run them, I tried the following:
$somearray['someclass']->$somearray['somemethod']();
usign this I am getting the following error:
Fatal error: Method name must be a string in ......................
Anyone have an idea how to do this?
+2
a source to share
5 answers
I cannot reproduce the error with the code provided (as @webbiedave pointed out the syntax is correct).
However, you can put the method name on a string before using it to ensure that the method name is a string. This ensures that even it is a user-defined object with a method __toString()
, it will be converted to its string representation.
$somearray['someclass']->{(string)$somearray['somemethod']}();
0
a source to share