Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重新处理未知数量的参数_Php_Arguments - Fatal编程技术网

Php 重新处理未知数量的参数

Php 重新处理未知数量的参数,php,arguments,Php,Arguments,我有一个小函数,用来调用类中的一些函数 public static function call($method){ $Model = new Model(); if(method_exists($Model, $method)){ return $Model->{$method}(); } } 现在,我的问题是关于通过的ARG。我想重新通过它们,我不想传递数组,而是要传递实际的参数 我知道函数func_get_arg()和func_num_args

我有一个小函数,用来调用类中的一些函数

public static function call($method){
    $Model = new Model();
    if(method_exists($Model, $method)){
        return $Model->{$method}();
    }
}
现在,我的问题是关于通过的ARG。我想重新通过它们,我不想传递数组,而是要传递实际的参数

我知道函数func_get_arg()和func_num_args(),但这不起作用:

$args = '';
for($i=0; $i<func_num_args(); $i++){
    $args .= func_get_args($i).',';
}

$args = substr($args, 0, strlen($args)-1);
如果我这样做,它会起作用,因为直到现在我只有一个arg或没有:

public static function call($method, $args = null){
    $Model = new Model();

    if(method_exists($Model, $method)){
        return $Model->{$method}($args);
    }
}
解决方案:

当然,我必须更改方法调用:

public static function call(){
    $Model = new Model();

    $args = func_get_args();
    $method = array_shift($args);

    if(method_exists($Model, $method)){
        return call_user_func_array(array($Model, $method), $args);
    }
}

以上工作。谢谢。

谢谢,现在可以用了:)
function foo() {
    $args = func_get_args();
    return call_user_func_array(array($model, $method), $args);
}
function foo() {
    $args = func_get_args();
    return call_user_func_array(array($model, $method), $args);
}