2013-10-29 3 views
2

프로젝트에서 작업하고 있는데 'lazy-load'객체를 사용하려고합니다.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