Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate non existing method error in __call?

Tags:

php

Maybe strange question, but... I have a magic __call method, that return instances of certain classes, or, if there are no such class, calls same method in underlying object.

public function __call($name, $arguments) {     $class = 'My\\Namespace\\' . $name;      if (class_exists($class, true)) {          $reflect = new \ReflectionClass($class);         return $reflect->newInstanceArgs($arguments);      } elseif (is_callable([$this->connector, $name])) {          return call_user_func_array([&$this->connector, $name], $arguments);     } else {         // ????     } } 

But what to do in else block? Can I simulate undefined method error? Or what exception to throw would be correct?

like image 621
Terion Avatar asked Jun 02 '13 21:06

Terion


1 Answers

You can trigger PHP errors manually using trigger_error:

trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR); 

See http://php.net/manual/en/function.trigger-error.php

like image 100
haim770 Avatar answered Oct 02 '22 18:10

haim770