php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50699 Cannot throw Exceptions in __toString()
Submitted: 2010-01-08 22:22 UTC Modified: 2012-12-05 01:19 UTC
From: gmblar+php at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.1 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 34 = ?
Subscribe to this entry?

 
 [2010-01-08 22:22 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-08 22:24 UTC] 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:51 UTC] 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.
 [2012-12-04 13:05 UTC] 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.
 [2012-12-05 01:18 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;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-05 01:19 UTC] aharvey@php.net
I have updated the manual to note the issue, so consider it documented. :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC