2012-03-15 2 views
0

私は__callを使用するクラス()があります。arrayを返す__call()の返すメソッドはなぜですか?

public function __call($method, $args){ 
    $method = "_".$method; 
    if (method_exists($this, $method)) { 
     try { 
      return $this->$method($args); 
     } 
     catch (Validation_exception $e) { 
      $this->exceptions[] = $e->getMessage(); 
      return; 
     } 
    } 
} 

しかし、それは、私は、文字列を返すメソッドがあっても私の配列を返します:私がやるのであれば

protected function _return_string(){ 
    return "string"; 
} 

を:
を echo $ myclass-> return_string(); print_r($ myclass-> return_string());

それが出力:
アレイ()
アレイ([0] => "文字列")

なぜ配列を返しありません? //出力

+1

[できません](http://codepad.org/PdXOp95k) – Gordon

+1

おそらく、奇妙なPHPプリプロセッサが、セミコロンではない ':'を修正しようとしているかもしれません。 – Paulpro

+0

これは誤った形式でした。 – localhost

答えて

0
class myClass { 
    protected function _return_string($arg){ 
     return is_array($arg) ? $arg[0] : $arg; 
    } 

    public function __call($method, $args){ 
     $method = "_".$method; 
     if (method_exists($this, $method)) { 
      try { 
       return $this->$method($args); // $args passed as an array here 
      } 
      catch (Validation_exception $e) { 
       $this->exceptions[] = $e->getMessage(); 
       return; 
      } 
     } 
    } 
} 

$foo = new myClass(); 
echo $foo->__call('return_string','Hello'); // Prints "Hello" 
echo $foo->return_string(' World'); //Prints "World" 

画面上の "Hello World"

注:リターン$この - > $法($ argsが)配列として$ argsをを通過しました。

関連する問題