douzhenyu6533 2012-11-21 09:29
浏览 38

将不确定数量的参数传递给函数 - PHP [重复]

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.

  • 写回答

1条回答 默认 最新

  • douxuanling6523 2012-11-21 09:35
    关注

    use func_num_args it will give number of function arguments.just use it and use the arguments like this example.

    <?php
    function foo()
    {
       $numargs = func_num_args();
       echo "Number of arguments: $numargs
    ";
    }
    
    foo(1, 2, 3);   
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭