Exception in magic method

Is that possible to declare throwed exception in magic method?..

@method Foo get()

get() isnt a magic method. __get() is. You can overload __get(), so presumably you can throw from within the overload as much as you could from any other function…

There is the magic method __call(). It throws some exception. And there are the “virtual” method declarations for __call(). They called also “magic methods”. Something like…

@method Foo getSomething()
@method Bar getSomethingElse()

My question is: is that possible to declare throwed exceptions in methods like getSomething()

To steal from Rudy…

“What happened when you tested it?”

The method docs don’t mention exceptions.

Out of curiosity I tried:

/**
 * @method int getMe()
 * @throws Exception
 */
class Something {

    /**
     * @return int
     * @throws Exception
     */
    public function xxx() : int
    {
        return 0;
    }
}

$s = new Something();

// Indicates exception thrown but no unhandled warning
$s->getMe();

// Unhandled exception warning
$s->xxx();

Using PHPStorm I can see that getMe throws an exception when I hover over getMe. However, when I call getMe without a try/catch block I do not get the expected unhandled exception warning.

No idea what any generated docs would show.

As far as I know you still can’t declare exceptions as part of a method signature inside of php itself. It’s strictly a phpdoc sort of thing.

That’s right. Syntax * @method getTest() @throws Exception is valid, but if I type somewhere…

    public function get()
    {
        return $this->getTest();
    }

I’ll see no unhandled exception warning.

And more badly, if I use my own Exception and type…

    public function get()
    {
        try {
            $this->getTest();
        } catch (MyException $exception) {}
    }

I’ll see warning Exception 'MyException' never thrown...