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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phofstetter at sensational dot ch
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Thu Oct 31 22:01:27 2024 UTC