tags:

views:

283

answers:

3

Why it's impossible to throw exception from __toString()?

class a
{
    public function __toString()
    {
        throw new Exception();
    }
}

$a = new a();
echo $a;

the code above produces this:

Fatal error: Method a::__toString() must not throw an exception in /var/www/localhost/htdocs/index.php on line 12

I was pointed to http://php.net/manual/en/migration52.incompatible.php where this behavior is described, but why? Any reasons to do that?

May be anyone here knows this?

At bug tracker php-dev-team as usual says nothing but "rtfm": http://bugs.php.net/50699

+1  A: 

My guess would be that __toString is hackish and therefore exists outside of the typical stack. A thrown exception, then, wouldn't know where to go.

Matchu
yes, the hackish nature is the most appropriate reason imho.
zerkms
especially since `echo $a->__toString()` can throw an exception, but `echo $a` cannot.
Wallacoloo
+1  A: 

I don't think the rationale for this decision has ever been publicized. Looks like some internal architectural limitation.

On a more abstract level, it kind of makes sense. An object should be able to return a string representation of itself, no reason for that kind of action to fail.

Yuval A
"An object should be able to return a string representation of itself, no reason for that kind of action to fail." hehe :-) echo $a->__toString(); throws it ;-) and this is expected. in this case method also should be able to return something casted to string, but no, we get unhandled exception.
zerkms
The exception you get is not thrown from within `__toString()`
Yuval A
But i mean "An object should be able to return a string representation of itself" should be true also for "a method should be able to return something to echo" as you mentioned. but it's false. ps: the exception i got was thrown from __toString()
zerkms
No it was not, you are receiving an exception when trying to invoke a method which does not exist on non-object `$a`
Yuval A
class a{ public function __toString() { throw new Exception(); }}$a = new a();echo $a->__toString();huh? when object not exists it's a fatal error (and notice), not an exception
zerkms
+6  A: 

After a couple searches I found this, which says:

Johannes explained that there is no way to ensure that an exception thrown during a cast to string would be handled correctly by the Zend Engine, and that this won't change unless large parts of the Engine are rewritten. He added that there have been discussions about such issues in the past, and suggested that Guilherme check the archives.

The Johannes referenced above is the PHP 5.3 Release Manager, so it's probably as "official" an explanation as you might find as to why PHP behaves this way.

The section goes on to mention:

__toString() will, strangely enough, accept trigger_error().

So not all is lost in terms of error reporting within __toString().

Rob Hruska
heh, thanx. but trigger_error() cannot replace try/catch just because it's global and try/catch is concrete.
zerkms
@zerkms - It's true, it's no replacement. Maybe if enough people voice their opinions they'll rewrite the Zend Engine. :)
Rob Hruska