Can I do the following in PHP?
$lstrClassName = 'Class';
$lstrMethodName = 'function';
$laParameters = array('foo' => 1, 'bar' => 2);
$this->$lstrClassName->$lstrMethodName($laParameters);
The solution I’m using now, is by calling the function with eval() like so:
eval('$this->'.$lstrClassName.'->'.$lstrMethodName.'($laParameters);');
I’m curious if there is a beter way to solve this.
Thanks!
You don’t need eval to do that … depending on your version
Examples
Output
And if you are using PHP 5.4 you can just call it directly without having to declare variables
You might also want to look at
call_user_funchttp://php.net/manual/en/function.call-user-func.php