php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70967 Weird error handling for __toString when Error is thrown
Submitted: 2015-11-25 02:39 UTC Modified: 2015-11-29 03:50 UTC
From: bugs dot php dot net at majkl578 dot cz Assigned: laruence (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.0RC7 OS:
Private report: No CVE-ID: None
 [2015-11-25 02:39 UTC] bugs dot php dot net at majkl578 dot cz
Description:
------------
When an error occurs inside __toString method, for example call to an undefined function, an Error is thrown. But since __toString does not allow exceptions to be thrown from inside, it just says "__toString() must not throw an exception".

This behavior is especially confusing e.g. in case of a typo (see below).

Test script:
---------------
<?php

echo new class {
	function __toString() {
		return stravl(123); // should be strval
	}
};

Expected result:
----------------
Something more accurate, either the message of the Error itself or at least better generic message.

Actual result:
--------------
Fatal error: Method class@anonymous::__toString() must not throw an exception

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-25 05:13 UTC] laruence@php.net
hmm, then what should it says? must not throw anything? :)
 [2015-11-25 09:33 UTC] laruence@php.net
okey , I understand it now.. but I am not sure how to fix it is better :<
 [2015-11-29 02:09 UTC] shiranai7 at hotmail dot com
Doesn't zend_std_cast_object_tostring() have access to the exception object? If it does, the exception's message and stack trace could be appended to the fatal error's message. (This is already the default behavior for uncaught exceptions).

Example:

Method FooBar::__toString() must not throw an exception. Uncaught exception 'Exception' with message 'BLAH' in SOMEFILE:123
Stack trace:
#0 {main}
  thrown in SOMEFILE on line 123
 [2015-11-29 03:50 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2015-11-29 03:50 UTC] laruence@php.net
hmm, okey, let me think about it
 [2015-11-29 07:44 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9397f527248a948e4d3929adb119e7e3519ce5b8
Log: Fixed Bug #70967 (Weird error handling for __toString when Error is thrown)
 [2015-11-29 07:44 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2016-07-20 11:35 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9397f527248a948e4d3929adb119e7e3519ce5b8
Log: Fixed Bug #70967 (Weird error handling for __toString when Error is thrown)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC