1

大家好,大家好,

我编写了一个脚本,它读取包含一行或多行的字符串。然后将在数组中读取这些行以进行循环。每行包含一个类和一个在 foreach 中调用的方法。调用方法的结果保存在要返回的 var 中。

我现在的问题是,在返回我该方法不存在之前,只有最后一个调用被执行所有其他调用,即使我改变了行的顺序,最后一个调用总是有效的。这包括所有方法都在那里并且有效。

线条看起来像这样

class_1/method1 class_2/method2 class_2/method1

我用 foreach 循环的给定数组看起来像这样

大批(

[0] => class_1/method1 [1] => class_2/method2 [2] => class_2/method1

现在我的代码像这样转换新数组中的每个项目

大批(

[0] => class_1 [1] => 方法1

在其中调用类 class_1 和 method1

我的代码是这个

    public function execute_lines($f){
     $cont = "";  // contains the results of all calls

     if($l = $this->get_line_array($f)){  // $l contains the array of all lines
        foreach($l as $k => $v){        
        if(strpos($v,"/")){     
         $a = explode("/",$v);  // $a contanis the array with the class and method and may be further data to be used in the methods called
         $c = ucfirst($a[0]);   // var of the Class
         $m = strtolower($a[1]);  // var of the methode
         unset($a[0],$a[1]);  // delete the first two items so that the array contains only further data

         if(method_exists($c,$m)){  // see if the method exists
            $x = new $c();  // instantiate the Class
            $cont .=  $x->e($m,$a);  // save result 
            print "-<br />";  // control if the method exits
         }else{
            print "/<br />";  // control if the method does not exists 
         }
        }
     }
    }   
    return $cont;  // returns the cont
    }

结果是

"/" "/" 然后只有最后一次调用的内容应该出现的地方

谢谢你的帮助

4

1 回答 1

1

如果您正在从文件中读取行,那么每行都会附加一个换行符(似乎只有最后一行没有)

更改您的代码并重试

foreach($l as $k => $v){     
    $v = trim($v); // trim whitespace

    if(strpos($v,"/")){
        ...
    }
}
于 2012-06-20T14:27:21.310 回答