是否可以以某种方式将_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");
答案 0 :(得分:0)
好的,事实证明,我要求的是可能的。这是固定代码:
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!