Req #53648 [Com]: Allow __toString() to throw exceptions

From: Date: Mon, 08 Sep 2014 09:25:29 +0000
Subject: Req #53648 [Com]: Allow __toString() to throw exceptions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187440@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53648&edit=1

 ID:                 53648
 Comment by:         benjamin dot morel at gmail dot com
 Reported by:        clicky at erebot dot net
 Summary:            Allow __toString() to throw exceptions
 Status:             Suspended
 Type:               Feature/Change Request
 Package:            Class/Object related
 PHP Version:        5.3.4
 Block user comment: N
 Private report:     N

 New Comment:

By the way, it's interesting to note that calling __toString() manually works fine:

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

echo (new A); // fatal error
echo (new A)->__toString(); // exception

Still no news on that issue, 2 years later? It's a very big blocking issue for doing
interesting things in modern frameworks.


Previous Comments:
------------------------------------------------------------------------
[2012-12-05 01:13:22] aharvey@php.net

Suspending: as Stas explained in the linked e-mail thread, it's "pretty hard" to do
in the current engine.

Should there ever be a Zend Engine 3, however, this could be revisited.

------------------------------------------------------------------------
[2012-12-04 17:15:19] benjamin dot morel at strictcoding dot co dot uk

@nikic: too bad, I did not imagine that this was a problem with PHP internals, but 
rather an architectural choice. Can we keep this feature request open, until 
someone finds a solution? It would be good to keep this as a target!

------------------------------------------------------------------------
[2012-12-04 16:41:50] nikic@php.net

@benjamin: I don't think anyone opposes this feature request, it's just too hard to
implement. Allowing __toString to throw exceptions would require any internal code doing a zval to
string conversion to be exception-safe. See also: http://markmail.org/message/lkpmpnhrvcbafhkd

------------------------------------------------------------------------
[2012-12-04 12:57:36] benjamin dot morel at strictcoding dot co dot uk

I strongly support this feature request.
I haven't read any valid supportive argument so far, for keeping this behaviour as 
it is currently.
In case someone would be worried about backward compatibility issues, this change 
could be introduced with the next major version, PHP 5.5!

I would be grateful if a member of the PHP team could review this request.

------------------------------------------------------------------------
[2011-11-10 04:05:48] dqhendricks at hotmail dot com

this problem is causing me such a headache. i'm not even trying to convert an object 
to a string and i'm getting this error. the bug become such a pain to track. i have 
an active record that lazy loads child objects through __get. so even though im just 
trying to print a property of an object that is a string, the fact that a __get is 
used to get that object, prevents me from throwing exceptions. it looks like this 
echo $activerecord->childobject->somestring;

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=53648


--
Edit this bug report at https://bugs.php.net/bug.php?id=53648&edit=1


Thread (14 messages)

« previous php.bugs (#187440) next »