I have an array populated by several class methods such that, I can randomly call some those methods for testing. The code below is what I'm trying to make work, but, I'm only getting the name of the method, it's not executed.
For example, lets say 1 is returned to $methodIndex, I get back func2() instead of Hello Func2.
Is there a function in PHP to do this or simple workaround?
class A{
public function func1() { echo "Hello Func1"; }
public function func2() { echo "Hello Func2"; }
public function func3() { echo "Hello Func3"; }
private $methods = ['func1', 'func2', 'func3'];
public function get_methods(){ return $methods; }
}
$object = new A();
$methodIndex = mt_rand(0, count($object->get_methods()) - 1);
$object->get_methods()[$methodIndex]."()"; //e.g. $object->func2();