这就是我目前所拥有的: p>
public function setUp (){
$ tests = array(
'printHello'=> array(),
'printWorld'=> array(),
'printName'=> array('Bob'),\ n);
}
公共函数printHello(){
echo“Hello,”;
}
公共函数printWorld(){
echo“World!”;
}
public function printName($ name = false){
echo $ name;
}
code> pre>
我想要做的是遍历所有函数并连续运行它们。 这就是我最终要做的事情,但似乎应该有一种更有效的方法来做到这一点: p>
foreach($ tests as $ test => $ parameters){
$ num_params = count($ parameters);
switch($ num_params){
case 0:
$ this-> $ test();
break;
case 1:\ n $ this-> $ test($ parameters [0]);
break;
case 2:
$ this-> $ test($ parameters [0],$ parameters [1]);
break;
case 3:
$ this-> $ test($ parameters [0],$ parameters [1],$ parameters [2]);
break;
default:
echo“错误! 超过3个参数的功能“。 $ test。 “!”;
退出;
}
}
code> pre>
我使用的是PHP 5.3。 有没有更有效的方法在循环中调用函数? p>
编辑:我不能 strong>使用 call_user_func_array code>,因为我在父类中调用非静态方法。 还有另外一种方法吗? p>
div>