30

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?

4

1 回答 1

30

您可以使用以下方法手动触发 PHP 错误trigger_error

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

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

于 2013-06-02T21:35:32.753 回答