RE: [PHP-DEV] Allow __toString() to throw exceptions
| From: | Thomas Punt | Date: | Sun, 24 May 2015 14:31:32 +0000 |
| Subject: | RE: [PHP-DEV] Allow __toString() to throw exceptions | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-86363@lists.php.net to get a copy of this message | ||
Hey,
> I have a question: is there any reason to throw an exception from
> __toString, *other* than a non-recoverable error, which would denote that
> the object, which the __toString method is called on, does not have any
> meaningful string representation? Because if not, core implementation fully
> serves its purpose throwing Fatal error now, even if it is non-standard way.
The problem with not enabling __toString to throw exceptions is that if any
exceptions thrown are by the engine itself (for things like uncallable functions),
then the method returns an unrelated E_ERROR regarding the inability to
throw exceptions.
The code example [1] shows this where the old message was:
"Fatal error: Call to undefined function a() in..."
and now after the inclusion of exceptions in the engine, it is:
"Fatal error: Method Test::__toString() must not throw an exception in..."
This is obviously not an overly helpful response, and the E_ERROR cannot be
caught either.
Thanks,
Tom
[1] http://3v4l.org/eGc1d