1

みなさん、こんにちは。

1 行以上の文字列を読み取るスクリプトを作成しました。これらの行は、配列で読み取られてループします。各行には、foreach で呼び出されるクラスとメソッドが含まれています。呼び出されたメソッドの結果は var に保存されて返されます。

私の問題は、メソッドが存在しないことを返す前に、最後の呼び出しのみが実行され、他のすべての呼び出しが実行されることです。それでも、行の順序を変更しても、常に最後の呼び出しが機能します。これには、すべてのメソッドが存在し、機能することが含まれます。

線は次のようになります

クラス_1/メソッド1 クラス_2/メソッド2 クラス_2/メソッド1

foreachでループする特定の配列は次のようになります

配列(

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

今私のコードは、このように新しい配列の各項目を変換します

配列(

[0] => クラス_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 に答える