2

私はプロジェクトに取り組んでおり、オブジェクトを「遅延ロード」しようとしています。

Magic Method __call($name, $arguments) を使用して単純なクラスをセットアップしました。

私がやろうとしているのは、 $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);

}

ただし、上記によって実際に呼び出されるメソッドでは、$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

これは理にかなっていますか、私がやろうとしていることを達成する方法を知っている人はいますか?

4

1 に答える 1

3

試す:

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

それ以外の:

$this->$name = call_user_func($class.'::factory', $arguments);
于 2013-10-29T11:36:42.477 に答える