php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78505 casting stdClass to string now unrecoverable fatal error
Submitted: 2019-09-06 12:02 UTC Modified: 2019-09-06 12:41 UTC
From: phofstetter at sensational dot ch Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.0RC1 OS: macOS, Debian Linux
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:
31 - 22 = ?
Subscribe to this entry?

 
 [2019-09-06 12:02 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-06 12:12 UTC] 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:16 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2019-09-06 12:16 UTC] 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:40 UTC] bugreports at gmail dot com
it's catchable since PHP 7.0

try
{
 
}
catch(Throwable $exception)
{
 
}
 [2019-09-06 12:41 UTC] phofstetter at sensational dot ch
haha! PBKAC. Sorry for wasting your time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 23:01:32 2024 UTC