2013-10-29 28 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