title: passing an undetermined number of arguments to a function - PHP
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5009382/unlimited-arguments-for-php-function">Unlimited arguments for PHP function?</a><br>
    <a href="https://stackoverflow.com/questions/6422075/forward-undefined-number-of-arguments-to-another-function">Forward undefined number of arguments to another function</a>  </p>
  </blockquote>
  
  
  
  <p>I am setting up a Gearman server in order to 'delegate' the execution of a method on an object, like for example :</p>
  
  <pre><code>$user-&gt;synchronize();
  </code></pre>
  
  <p>or </p>
  
  <pre><code>$event-&gt;publish('english', array('remote1','remote2') );
  </code></pre>
  
  <p>(where remote1 and remote2 are remote social networks)</p>
  
  <p>My idea is to wrap the object, the method name, and the arguments (also with some other parameters like the language) into an object that I can serialize and send to the gearman worker, with something like this :</p>
  
  <pre><code> class bzkGearmanWrapper {
          public $object;
          public $method;
          public $args; 
          /*
          * @param  $object (object) any object
          * @param  $method (string) the name of the method to execute
          * @param  $args    an argument or an array containing the arguments to pass to the method
          */
  
      private function __construct($object, $method, $args )  { 
          $this-&gt;object = $object;
          $this-&gt;method = $method;
          $this-&gt;args = $args;
      }
  
      private function execute() {
          $object = $this-&gt;object;
          $method = $this-&gt;method;
          $args   = $this-&gt;args;
          return $object-&gt;{$method}($args);
  
      }
  }
  </code></pre>
  
  <p>then I would be able to do in my main script </p>
  
  <pre><code>$client =new GearmanClient();
  
  // instead of :  $user-&gt;synchronize();
  $params = new bzkGearmanWrapper($user, 'synchronize');
  $client-&gt;do('execute', $params);
  
  // instead of : $event-&gt;publish('english', array('remote1','remote2') );
  $targets = array('remote1', 'remote2');
  $params = new bzkGearmanWrapper($event, 'publish', array('english', $targets); 
  $client-&gt;do('execute', $params);
  </code></pre>
  
  <p>and in my gearman worker, i can simply call a 'execute' task like</p>
  
  <pre><code>function execute($job) {
      $wrapper = unserialize( $job-&gt;workload() ); 
      return $wrapper-&gt;execute(); 
  }
  </code></pre>
  
  <p>The method execute above will work if I am give a single argument, but how can I do if I need to give undetermined number of arguments. Most of my method using maximum 2 arguments, I could write </p>
  
  <pre><code>return $object-&gt;{$method}($arg1, $arg2);
  </code></pre>
  
  <p>One solution is to use eval(),but I would prefer to avoid it.</p>
  
  <p>Do you know any way to pass the arguments to a function ?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>this topic has been closed as being a duplicate of 2 older topics. The first one was about the call_user_func_array() function which would do the job for a user function, but not for an object. The second topic <a href="https://stackoverflow.com/questions/6422075/forward-undefined-number-of-arguments-to-another-function">Forward undefined number of arguments to another function</a> mentions the use of ReflectionClass. I did some homeworks, and here is the outcome using <a href="http://www.php.net/manual/en/reflectionmethod.invokeargs.php" rel="nofollow noreferrer">ReflectionMethod::invokeArgs</a>. </p>
  
  <pre><code>class bzkObjectWrapperException extends Exception { }
  
  class bzkObjectWrapper {
  
      public $object;
      public $method;
      public $args; 
  
      public function __construct($object, $method, $args = array() )  { 
          $this-&gt;object = $object;
          $this-&gt;method = $method;
          $this-&gt;args = $args;
      }
  
      public function execute() {
  
          $object = $this-&gt;object;
          $method = $this-&gt;method;
          $args   = is_array($this-&gt;args) ? $this-&gt;args : array($this-&gt;args);
          $classname = get_class($object);
  
          $reflectionMethod = new ReflectionMethod($classname, $method);
          return $reflectionMethod-&gt;invokeArgs($object, $args);   
      }
  }
  </code></pre>
  
  <p>Hope it can help. And thanks for the links to the second topic.</p>