Possible Duplicate:
Unlimited arguments for PHP function?
Forward undefined number of arguments to another function
I am setting up a Gearman server in order to 'delegate' the execution of a method on an object, like for example :
$user->synchronize();
or
$event->publish('english', array('remote1','remote2') );
(where remote1 and remote2 are remote social networks)
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 :
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->object = $object;
$this->method = $method;
$this->args = $args;
}
private function execute() {
$object = $this->object;
$method = $this->method;
$args = $this->args;
return $object->{$method}($args);
}
}
then I would be able to do in my main script
$client =new GearmanClient();
// instead of : $user->synchronize();
$params = new bzkGearmanWrapper($user, 'synchronize');
$client->do('execute', $params);
// instead of : $event->publish('english', array('remote1','remote2') );
$targets = array('remote1', 'remote2');
$params = new bzkGearmanWrapper($event, 'publish', array('english', $targets);
$client->do('execute', $params);
and in my gearman worker, i can simply call a 'execute' task like
function execute($job) {
$wrapper = unserialize( $job->workload() );
return $wrapper->execute();
}
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
return $object->{$method}($arg1, $arg2);
One solution is to use eval(),but I would prefer to avoid it.
Do you know any way to pass the arguments to a function ?
EDIT
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 Forward undefined number of arguments to another function mentions the use of ReflectionClass. I did some homeworks, and here is the outcome using ReflectionMethod::invokeArgs.
class bzkObjectWrapperException extends Exception { }
class bzkObjectWrapper {
public $object;
public $method;
public $args;
public function __construct($object, $method, $args = array() ) {
$this->object = $object;
$this->method = $method;
$this->args = $args;
}
public function execute() {
$object = $this->object;
$method = $this->method;
$args = is_array($this->args) ? $this->args : array($this->args);
$classname = get_class($object);
$reflectionMethod = new ReflectionMethod($classname, $method);
return $reflectionMethod->invokeArgs($object, $args);
}
}
Hope it can help. And thanks for the links to the second topic.