Req #53648 [NEW]: Allow __toString() to throw exceptions
From:
Operating system:
PHP version: 5.3.4
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:Allow __toString() to throw exceptions
Description:
------------
Currently, when casting an object with __toString() to a string,
__toString() is not allowed to throw an exception.
Trying to do so triggers an E_ERROR "Method %s::__toString() must not throw
an exception".
IMHO, this is counter-intuitive, especially since calling that object's
__toString() method directly would correctly throw the exception.
I propose to allow __toString() to throw exceptions.
Note: this was already reported in http://bugs.php.net/bug.php?id=50699 but
marked as bogus with the only explanation being "__toString must not
throw".
So, I'm marking this as a feature request rather than a bug, since the
current behaviour seems to be expected (though it is not documented in
http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring).
I also found this entry
http://stackoverflow.com/questions/2429642/why-its-impossible-to-throw-exception-from-tostring
which cites Johannes saying this requires major changes in the code.
However, the article mentioned is from 2007 and I think the limitations in
ZE that prevented this to be implemented were lifted since then.
I'm not very versed in PHP's inner workings, but attached is a tentative
patch which seems to fix this issue.
Test script:
---------------
<?php
class CustomException
extends Exception
{
}
class Foo
{
public function __toString()
{
throw new CustomException('oops!');
}
}
$foo = new Foo();
try {
var_dump((string) $foo);
}
catch (CustomException $e) {
var_dump($e);
}
?>
Expected result:
----------------
object(CustomException)#2 (7) {
["message":protected]=>
string(5) "oops!"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(17) "/tmp/toString.php"
["line":protected]=>
int(65)
["trace":"Exception":private]=>
array(1) {
[0]=>
array(6) {
["file"]=>
string(17) "/tmp/toString.php"
["line"]=>
int(71)
["function"]=>
string(10) "__toString"
["class"]=>
string(3) "Foo"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
}
["previous":"Exception":private]=>
NULL
}
Actual result:
--------------
PHP Fatal error: Method Foo::__toString() must not throw an exception in
/tmp/toString.php on line 0
PHP Stack trace:
PHP 1. {main}() /tmp/toString.php:0
--
Edit bug report at http://bugs.php.net/bug.php?id=53648&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=53648&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=53648&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=53648&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=53648&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53648&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=53648&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=53648&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=53648&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=53648&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=53648&r=support
Expected behavior: http://bugs.php.net/fix.php?id=53648&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=53648&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=53648&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=53648&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53648&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53648&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=53648&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=53648&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=53648&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=53648&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=53648&r=mysqlcfg
Thread (14 messages)
- clicky at erebot dot net
- dqhendricks at hotmail dot comReq #53648 [Com]: Allow __toString() to throw exceptions
- benjamin dot morel at strictcoding dot co dot ukReq #53648 [Com]: Allow __toString() to throw exceptions
- nikic@php.netReq #53648 [Com]: Allow __toString() to throw exceptions
- benjamin dot morel at strictcoding dot co dot ukReq #53648 [Com]: Allow __toString() to throw exceptions
- aharvey@php.netReq #53648 [Opn->Sus]: Allow __toString() to throw exceptions
- benjamin dot morel at gmail dot comReq #53648 [Com]: Allow __toString() to throw exceptions
- ianbytchek at gmail dot comReq #53648 [Com]: Allow __toString() to throw exceptions
- benjamin dot morel at gmail dot comReq #53648 [Com]: Allow __toString() to throw exceptions
- benjamin dot morel at gmail dot comReq #53648 [Com]: Allow __toString() to throw exceptions
- aharvey@php.netReq #53648 [Sus->Opn]: Allow __toString() to throw exceptions
- simohe at besonet dot chReq #53648 [Com]: Allow __toString() to throw exceptions
- carusogabriel@php.netReq #53648 [Com]: Allow __toString() to throw exceptions
- nikic@php.netReq #53648 [Asn->Csd]: Allow __toString() to throw exceptions