2011-01-31 19 views
0

私はヘルパーがほとんどありません - それぞれのヘルパーのメソッドをラムダ無名関数として赤くしたいと思います。eval()からラムダ関数を生成する

私はヘルパー・メソッドを取得した後、eval関数を実行して、それをやろうとしている

、それ文句を言わない仕事、イム取得パースエラー..

私の現在のコード:

  foreach($this->helpers as $helper) 
      { 
       include(master_path . 'helpers/'.$helper.'Helper.php'); 

       $helperClass = new applicationHelper(); 
       $methods = get_class_methods($helperClass); 
       foreach($methods as $method) 
       { 

        eval ("\$$method = function (\$text) { 
         \$helperClass->$method(\$text); 
        }"); 

       } 
      } 

効率の恐怖のために - あなたがそれを知っていれば、私はより良い解決をしたいと思います!ありがとう! ありがとうございました!動作するはず

+1

どのようにあなたの[クラス関数をラムダ関数に変換]と違うのですか?(http://stackoverflow.com/questions/4836258/translate-class-function-to-lambda-function) – Gordon

+0

私はあなたに尋ねていません私にそれをする方法を説明する - 私はすでに行ったが、そのバギー。とにかく、すでに私の答えを持って、ありがとう – WEBProject

答えて

2

foreach($methods as $method) 
{ 
    $$method = function($text) use ($method, $helperClass) { 
     return $helperClass->$method($text); 
    } 
} 

しかし、まだ、なぜあなたはそれをやっている知りません。

EDIT PHP 5.3.xの必要 - >こちらの遅いevalを取り除くAnonymous funcions

0
foreach ($this->helpers as $helper) { 
    include(master_path . 'helpers/'.$helper.'Helper.php'); 

    $helperClass = new applicationHelper(); 
    foreach (get_class_methods($helperClass) as $method) { 
     $$method = function($text) use($helperClass, $method) { 
      $helperClass->$method($text); 
     }; 
    } 
} 

を見て。

関連する問題