【问题标题】:passing an undetermined number of arguments to a function - PHP [duplicate]将不确定数量的参数传递给函数 - PHP [重复]
【发布时间】:2012-11-21 09:29:43
【问题描述】:

可能重复:
Unlimited arguments for PHP function?
Forward undefined number of arguments to another function

我正在设置一个 Gearman 服务器,以便“委托”执行对象上的方法,例如:

$user->synchronize();

$event->publish('english', array('remote1','remote2') );

(其中 remote1 和 remote2 是远程社交网络)

我的想法是将对象、方法名称和参数(也与语言等其他一些参数)包装成一个对象,我可以将其序列化并发送给 gearman 工作人员,如下所示:

 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);

    }
}

那么我就可以在我的主脚本中做

$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);

在我的齿轮工中,我可以简单地调用一个“执行”任务,例如

function execute($job) {
    $wrapper = unserialize( $job->workload() ); 
    return $wrapper->execute(); 
}

如果我给出一个参数,上面的方法执行将起作用,但是如果我需要给出不确定数量的参数,我该怎么办。我的大部分方法使用最多 2 个参数,我可以写

return $object->{$method}($arg1, $arg2);

一种解决方案是使用 eval(),但我宁愿避免使用它。

您知道将参数传递给函数的任何方法吗?

编辑

此主题已被关闭,因为它与 2 个旧主题重复。第一个是关于 call_user_func_array() 函数,它可以为用户函数完成工作,但不能为对象完成工作。第二个话题Forward undefined number of arguments to another function提到了ReflectionClass的使用。我做了一些功课,这是使用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);   
    }
}

希望对您有所帮助。并感谢您提供第二个主题的链接。

【问题讨论】:

    标签: php function arguments gearman


    【解决方案1】:

    使用func_num_args 它将给出函数参数的数量。只需使用它并使用此示例中的参数即可。

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

    【讨论】:

    • 谢谢,但我的问题不是处理函数内的参数,而是进行调用,因为我事先不知道是否必须执行 foo(1,2) 或 foo( 1,2,3) 或 foo(1,2,...,n)。问题是在调用函数时传递不确定数量的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2019-07-21
    • 2013-12-09
    • 2013-07-09
    • 2012-08-20
    相关资源
    最近更新 更多