I'm working on where I need to be able to pass an indexed array of arguments to a method, similar to how call_user_func_array works. I would use call_user_func_array , but this is not an OOP approach, which is undesirable, and it requires the method to be static, which splits the target OO class.
I tried using ReflectionClass , but to no avail. You cannot call arguments for a class method, but only for a constructor. This, unfortunately, is undesirable.
So, I took the pages with man and looked at the ReflectionFunction , but there is no way to create an instance of the class, point it to a method, and then invokeArgs with it.
An example of using ReflectionFunction (remember, this question is marked as PHP 5.4, therefore, the syntax):
$call = new \ReflectionFunction( "(ExampleClass())->exampleMethod" ); $call->invokeArgs( ["argument1", "argument2"] );
This fails:
Function (Index())->Index() does not exist
ReflectionMethod example
$call = new \ReflectionMethod( "ExampleClass", "exampleMethod" ); $call->invokeArgs( new ExampleClass(), ["argument1", "argument2"] ); print_r( $call );
This fails:
ReflectionMethod Object ( [name] => Index [class] => Index )
Arguments are never passed to the method.
Required Results:
class ExampleClass() { public function exampleMethod( $exampleArg1, $exampleArg2 ){
If I passed $array instance of ExampleClass->exampleMethod() , I would have only one argument, which would be an array. Instead, I need to be able to pull out individual arguments.
I thought that if there was a way to call ReflectorFunction on a ReflectorClass , I would do it in the form of a ship and on my way, but it does not look like it is possible.
Does anyone have something they used to accomplish this earlier?