doucan9079
2013-08-20 14:32
浏览 44
已采纳

PHP - 从数组调用函数?

This is what I currently have:

public function setUp() {
    $tests = array(
                'printHello'    =>  array(),    
                'printWorld'    =>  array(),
                'printName' =>  array('Bob'),
            );
}


public function printHello() {
echo "Hello, ";
}

public function printWorld() {
echo "World!";
}

public function printName($name=false) {
echo $name;
}

What I want to do is loop through all the functions and run them consecutively. This is what I had to end up doing, but it just seems like there should be a more efficient way to do this:

foreach($tests as $test=>$parameters) {
    $num_params = count($parameters);
    switch($num_params) {
        case 0:
            $this->$test();
            break;
        case 1:
            $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 "Error! More than 3 parameters for function" . $test . "!";
            exit;
    }       
}

I am using PHP 5.3. Is there a more efficient way to call the functions in a loop?

EDIT: I can't use call_user_func_array because I'm calling non-static methods from within their parent class. Is there another way?

图片转代码服务由CSDN问答提供 功能建议

这就是我目前所拥有的:

  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; 
} 
   
 
 

我想要做的是遍历所有函数并连续运行它们。 这就是我最终要做的事情,但似乎应该有一种更有效的方法来做到这一点:

  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。  “!”; 
退出; 
} 
} 
   
 
 

我使用的是PHP 5.3。 有没有更有效的方法在循环中调用函数?

编辑:我不能使用 call_user_func_array ,因为我在父类中调用非静态方法。 还有另外一种方法吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • duanfazhun0383 2013-08-20 14:35
    最佳回答

    You're looking for call_user_func_array():

    foreach($tests as $test=>$parameters) {
        call_user_func_array( array( $this, $test), $parameters);
    }
    

    Loading this into a simple test class:

    class Test {
        public function setUp() {
            $tests = array(
                'printHello'    =>  array(),    
                'printWorld'    =>  array(),
                'printName' =>  array('Bob'),
            );
            foreach( $tests as $fn => $params) 
                call_user_func_array( array( $this, $fn), $params);
        }
    
        public function printHello() {
            echo "Hello, ";
        }
    
        public function printWorld() {
            echo "World!";
        }
    
        public function printName($name=false) {
            echo $name;
        }
    }
    

    We can see that this outputs for PHP >= 5.0.0:

    Hello, World!Bob
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题