欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

如何自动生成一个参数可变的函数调用

程序员文章站 2022-05-30 12:53:19
...
代码思路如下:
public function __call($method, $args) {
return $this->redis->$method($args[0][,$args[1]][,$args[2]]...]);
}
请问如何实现 $this->redis->$method() 的参数列表根据 $args 变化而自动生成函数调用

回复内容:

代码思路如下:
public function __call($method, $args) {
return $this->redis->$method($args[0][,$args[1]][,$args[2]]...]);
}
请问如何实现 $this->redis->$method() 的参数列表根据 $args 变化而自动生成函数调用

给个思路

class RedisClient {
    protected $_redis;

    public function __construct() {
        $this->_redis = new Redis();
    }

    public function __call($method, $args) {
        call_user_func_array(array($this->_redis, $method), $args);
    }
}

不就是变长参数列表嘛。

相关标签: php