Example: I have an variable like
$method = "doSomething";
Assume that I have an $myObject which knows a nonstatic, public method doSomething()
Could I then just call that method by doing this?
$myObject->$method();
Or is there another / better way?
Yes, you can do that :
This is called Variable functions
And here is the code that proves it :
Gets you this output :
Which means the method has been called 😉
Another way would be to use
call_user_funcorcall_user_func_array:Here, you don’t need this — but you’d have to use that if your method was
static, at least for PHP <= 5.2