Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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_Parameters_Arguments_Call - Fatal编程技术网

将数组作为单个参数传递给php函数

将数组作为单个参数传递给php函数,php,parameters,arguments,call,Php,Parameters,Arguments,Call,解决方案 多亏了,我通过使用 public function __call($method, $params){ return call_user_func_array(array($this->_rpc, $method), $params); } 原始问题 为了便于在我的项目中使用,我创建了一个封装另一个类的类 jsonrpc使用公共函数调用($method,$params)来处理任何类型的泛型方法 我在我的类中也使用了相同的方法,但这意味着我必须将$params数组转换为单

解决方案

多亏了,我通过使用

public function __call($method, $params){
    return call_user_func_array(array($this->_rpc, $method), $params);
}
原始问题

为了便于在我的项目中使用,我创建了一个封装另一个类的类

jsonrpc使用
公共函数调用($method,$params)
来处理任何类型的泛型方法

我在我的类中也使用了相同的方法,但这意味着我必须将
$params
数组转换为单个变量,作为参数传入jsonrpc的
\u call()

如何将
$params
的数组更改为多个参数

在我的包装类中,我尝试使用
..
来提供参数,但它似乎只在使用
[]
实例化数组时起作用

一个简单的解决方案是编辑jsonrpc类如何处理它的参数,但我更愿意保持它的源代码不变

我所尝试的(显然,每个
\u调用都是单独尝试的,而不是同时尝试的…)

谢谢。

使用


使用
时出现了什么错误?@PHPWeblineindia这是一个语法错误:意外“.”,预期“.”。Nouplacep的想法是正确的,我必须使用
call\u user\u func\u数组(array($this->\u rpc$method),$params)但它起作用了。
class Wrap{
    //Could have been great, but doesn't work.
    public function __call($method, $params){
        $params = array_values($params);
        return $this->_rpc->$method(...$params);
    }

    //Horrible, but works
    public function __call($method, $params){
        switch (count($params)) {
            case 0:
                return $this->_rpc->$method();
                break;
            case 1:
                return $this->_rpc->$method($params[0]);
                break;
            case 2:
                return $this->_rpc->$method($params[0], $params[1]);
                break;
            case 3:
                return $this->_rpc->$method($params[0], $params[1], $params[2]);
                break;
            case 4:
                return $this->_rpc->$method($params[0], $params[1], $params[2], $params[3]);
                break;
            default:
                die("Horrible way doesn't have enough cases!");
                break;
        }
    }
}
public function __call($method, $params){
    return call_user_func_array($this->_rpc->$method(),$params)
}