2013-10-29 25 views
2

我正在研究一个项目,我想尝试'延迟加载'对象。PHP __call()魔法解析参数

我已经使用Magic Method __call($ name,$ arguments)设置了一个简单的类。

我试图做的是通过传递$参数,而不是一个数组,但由于变量列表:

public function __call($name, $arguments) 
{ 
    // Include the required file, it should probably include some error 
    // checking 
    require_once(PLUGIN_PATH . '/helpers/' . $name . '.php'); 

    // Construct the class name 
    $class = '\helpers\\' . $name;  

    $this->$name = call_user_func($class.'::factory', $arguments); 

} 

然而,在方法实际上是由上面,$叫参数作为数组传递,而不是单个变量EG

public function __construct($one, $two = null) 
{ 
    var_dump($one); 
    var_dump($two); 
} 
static public function factory($one, $two = null) 
{ 
    return new self($one, $two); 
} 

返回:

array 
    0 => string '1' (length=1) 
    1 => string '2' (length=1) 

null 

这是否有道理,没有人知道如何实现我想要什么?

回答

3

尝试:

$this->$name = call_user_func_array($class.'::factory', $arguments); 

代替:

$this->$name = call_user_func($class.'::factory', $arguments); 
+0

完美 - 这很好地工作 – Sjwdavies