Is it possible to call functions from class like this:
$class = new class;
$function_name = "do_the_thing";
$req = $class->$function_name();
Something similar solution, this doesn't seem to work?
Is it possible to call functions from class like this:
$class = new class;
$function_name = "do_the_thing";
$req = $class->$function_name();
Something similar solution, this doesn't seem to work?
You can use
ReflectionClass
.Example:
Remember to catch ReflectionException If Method Not Exist.
Yes, it is possible, that is know as variable functions, have a look at this.
Example from PHP's official site:
In your case, make sure that the function
do_the_thing
exists. Also note that you are storing the return value of the function:Try to see what the variable
$req
contains. For example this should give you info:Note:
Variable functions won't work with language constructs such as
echo(), print(), unset(), isset(), empty(), include(), require()
and the like. Utilize wrapper functions to make use of any of these constructs as variable functions.My easiest example is:
${$function_name} is the trick
Also works with static methods: