Bug #78505 [Com]: casting stdClass to string now unrecoverable fatal error
| From: | bugreports at gmail dot com | Date: | Fri, 06 Sep 2019 12:40:11 +0000 |
| Subject: | Bug #78505 [Com]: casting stdClass to string now unrecoverable fatal error | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222606@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78505&edit=1
ID: 78505
Comment by: bugreports at gmail dot com
Reported by: phofstetter at sensational dot ch
Summary: casting stdClass to string now unrecoverable fatal
error
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: macOS, Debian Linux
PHP Version: 7.4.0RC1
Block user comment: N
Private report: N
New Comment:
it's catchable since PHP 7.0
try
{
}
catch(Throwable $exception)
{
}
Previous Comments:
------------------------------------------------------------------------
[2019-09-06 12:16:46] nikic@php.net
This changed as part of https://wiki.php.net/rfc/tostring_exceptions.
It is *not* a fatal error, but an Error exception. You don't need to convert it into an
exception, it already is one.
------------------------------------------------------------------------
[2019-09-06 12:12:51] kontakt at beberlei dot de
For a workaround - and potentially the actual "fix", you should catch (Error $e) directly
in your code instead of relying on the error handler.
------------------------------------------------------------------------
[2019-09-06 12:02:58] phofstetter at sensational dot ch
Description:
------------
In previous releases of PHP, casting instances of stdClass to string would raise an
E_RECOVERABLE_ERROR, but in 7.4, this has changed to raise a E_ERROR fatal error.
I'm not entirely sure this increase of severity is entirely justified as catching fatal errors
in production is still annoying and hard to get meaningful backtraces reported.
Test script:
---------------
<?php
set_error_handler(function($errno, $errstr, $errfile, $errline){
if((error_reporting() & $errno) === 0){
return true;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
try {
(string)new stdClass;
} catch (ErrorException $e){
echo "caught\n";
}
Expected result:
----------------
"caught"
Actual result:
--------------
PHP Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in
/var/webapps/popscan/flupp.php:11
Stack trace:
#0 {main}
thrown in /var/webapps/popscan/flupp.php on line 11
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in
/var/webapps/popscan/flupp.php:11
Stack trace:
#0 {main}
thrown in /var/webapps/popscan/flupp.php on line 11
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78505&edit=1