PHP:将数组转换为函数参数列表


PHP: convert array to function argument list

是否可以以某种方式将_call中的数组转换为函数参数列表并使代码正常工作?

abstract class FooClass {
  protected function foo() {
    $args = func_get_args();
    $listIndex = 0;
    foreach ($args as $arg) {
      echo ++$listIndex . ": " . $arg . "'n";
    }
  }
}
class BarClass extends FooClass {
  public function __call($name, $arguments) {
    if (strcmp($name, 'foo') == 0) {
      $this->$name(list($arguments));
    }
    die("Unexpected method.");
  }
}
$barInstance = new BarClass;
$barInstance->foo("one", "two", "three", "four");

好吧,事实证明,我所要求的是可能的。这是固定代码:

abstract class FooClass {
  protected function foo() {
    $args = func_get_args();
    $listIndex = 0;
    foreach ($args as $arg) {
      echo ++$listIndex . ": " . $arg . "'n";
    }
  }
  protected function fooz() {
    echo "'nDone!'n";
  }
}
class BarClass extends FooClass {
  public function __call($name, $arguments) {
    if (in_array($name, array('foo', 'fooz'))) {
      call_user_func_array(array($this, $name), $arguments);
    } else {
      die("Unexpected method.");
    }
  }
}
$barInstance = new BarClass;
$barInstance->foo("one", "two", "three", "four");
$barInstance->fooz();

这是输出:

1: one
2: two
3: three
4: four
Done!