Edit report at https://bugs.php.net/bug.php?id=50699&edit=1
ID: 50699 Updated by: ahar...@php.net Reported by: gmblar+php at gmail dot com Summary: Cannot throw Exceptions in __toString() Status: Not a bug Type: Bug Package: *General Issues Operating System: * PHP Version: 5.3.1 Block user comment: N Private report: N New Comment: I have updated the manual to note the issue, so consider it documented. :) Previous Comments: ------------------------------------------------------------------------ [2012-12-05 01:18:52] ahar...@php.net Automatic comment from SVN on behalf of aharvey Revision: http://svn.php.net/viewvc/?view=revision&revision=328669 Log: Add a warning about the fatal error generated when you throw from __toString(). Prompted by bug #50699, although it doesn't really fix it as such. ------------------------------------------------------------------------ [2012-12-04 13:05:14] benjamin dot morel at strictcoding dot co dot uk @bjori The documentation does not say that __toString() must not throw an exception. The fact is, this is currently an undocumented behaviour, that could therefore be changed without causing much pain (we're supposed to write code based on the documented behaviour of a function, not its side effects): http://php.net/manual/en/language.oop5.magic.php#object.tostring A feature request has been open, I would be grateful if you could give your opinion there, especially now than PHP 5.5 is being developed: https://bugs.php.net/bug.php?id=53648 Thank you. ------------------------------------------------------------------------ [2010-01-08 22:51:46] gmblar+php at gmail dot com Current behavior of __toString is crap. Why i can use trigger_error() bug not throw Exception() in __toString()? Please fix it. ------------------------------------------------------------------------ [2010-01-08 22:24:09] bj...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php __toString() must not throw exceptions ------------------------------------------------------------------------ [2010-01-08 22:22:35] gmblar+php at gmail dot com Description: ------------ Cannot throw Exceptions in __toString(). Instead it produces a Fatal error. Reproduce code: --------------- <?php class bar { public function __toString() { throw new Exception('Incomplete Data'); } } $foo = new bar(); echo $foo; ?> Expected result: ---------------- Fatal error: Uncaught exception 'Exception' with message 'Incomplete Data' in /-:6 Actual result: -------------- Fatal error: Method bar::__toString() must not throw an exception in /- on line 0 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=50699&edit=1