Bug #50699 [Com]: Cannot throw Exceptions in __toString()
Edit report at https://bugs.php.net/bug.php?id=50699&edit=1
ID: 50699
Comment by: benjamin dot morel at strictcoding dot co dot uk
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:
@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.
Previous Comments:
------------------------------------------------------------------------
[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] bjori@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
Thread (5 messages)