2012-03-15 4 views
0

У меня есть класс, который использует __call():Почему метод возврата из возвращаемого массива __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());

Он будет:
Array()
Array ([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"

Примечание: возврата $ это -> $ метод ($ арг) прошло $ арг как массив.

Смежные вопросы