Dynamically calling an Args class method

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 ){ // do something here echo "Argument 1: {$exampleArg1}\n"; echo "Argument 2: {$exampleArg2}\n"; } } $array = [ 'exampleArg1Value', 'exampleArg2Value' ]; 

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?

+4
source share
3 answers

For some reason, something is stuck somewhere.

 $call = new \ReflectionMethod( "ExampleClass", "exampleMethod" ); $call->invokeArgs( new ExampleClass(), ["argument1", "argument2"] ); 

Now returns

 Argument 1: argument1 Argument 2: argument2 

I will try to reproduce the problem. It is on the new php 5.4.7 installed with php-cli and fpm.

+1
source

AFAIK, the following should work:

 $call = new \ReflectionMethod( "ExampleClass", "exampleMethod" ); $call->invokeArgs( new ExampleClass(), ["argument1", "argument2"] ); print_r( $call ); 

What is the minor version of PHP? Are you on 5.4.7?

+5
source

I wrote my own dependency injector, and I am also dynamically building classes with parameters. Here is the code your move should get:

 $type = 'ExampleClass'; $reflector = new \ReflectionClass( $type ); if ( !$reflector->isInstantiable() ) throw new \Exception( "Resolution target [$type] is not instantiable." ); $constructor = $reflector->getConstructor(); $parameters = $constructor->getParameters(); 

At this point, you have an array of parameters needed to build. Now you can replace the parameters with values ​​and build a class.

+3
source

Source: https://habr.com/ru/post/1436684/


All Articles