title: Dynamically Invoking Class Method Args
body: <p>I am working on something where I need to be able to pass an indexed array of args to a method, much like how <code>call_user_func_array</code> works. I would use <code>call_user_func_array</code> but it is not an OOP approach, which is undesired, and it requires the method to be static, which breaks the target class's OO.</p>
  
  <p>I have tried to use <code>ReflectionClass</code> but to no avail. You cannot invoke arguments to a method of the class, only the constructor. This is unfortunately, not desireable.</p>
  
  <p>So I took to the man pages and looked at <code>ReflectionFunction</code> but there is no way to instantiate the class, point it to a method, and then <code>invokeArgs</code> with it.</p>
  
  <p>Example using <code>ReflectionFunction</code> ( remember, this question is tagged PHP 5.4, hence the syntax):</p>
  
  <pre class="lang-php prettyprint-override"><code>$call = new \ReflectionFunction( "(ExampleClass())-&gt;exampleMethod" );
  $call-&gt;invokeArgs( ["argument1", "argument2"] );
  </code></pre>
  
  <p>This fails with:</p>
  
  <pre><code>Function (Index())-&gt;Index() does not exist
  </code></pre>
  
  <p>Example using <code>ReflectionMethod</code></p>
  
  <pre class="lang-php prettyprint-override"><code>$call = new \ReflectionMethod( "ExampleClass", "exampleMethod" );
  $call-&gt;invokeArgs( new ExampleClass(), ["argument1", "argument2"] );
  print_r( $call );
  </code></pre>
  
  <p>This fails with:</p>
  
  <pre><code>ReflectionMethod Object
  (
      [name] =&gt; Index
      [class] =&gt; Index
  )
  </code></pre>
  
  <p>The arguments are never passed to the method.</p>
  
  <p>The desired results are:</p>
  
  <pre class="lang-php prettyprint-override"><code>class ExampleClass() {
      public function exampleMethod( $exampleArg1, $exampleArg2 ){
          // do something here
          echo "Argument 1: {$exampleArg1}\n";
          echo "Argument 2: {$exampleArg2}\n";
      }
  }
  
  $array = [ 'exampleArg1Value', 'exampleArg2Value' ];
  </code></pre>
  
  <p>If I passed <code>$array</code> to an instance of <code>ExampleClass-&gt;exampleMethod()</code>, I would only have one argument, which would be an array. Instead, I need to be able to pull the individual arguments.</p>
  
  <p>I was thinking that if there was a way to call <code>ReflectorFunction</code> on a <code>ReflectorClass</code> I would in in ship-shape and on my way, but it doesn't look like that is possible.</p>
  
  <p>Does anyone have anything they have used to accomplish this previously?</p>