3v4l.org

run code in 300+ PHP versions simultaneously
<?php set_error_handler(array('My_ToStringFixer', 'errorHandler')); error_reporting(E_ALL | E_STRICT); class My_ToStringFixer { protected static $_toStringException; public static function errorHandler($errorNumber, $errorMessage, $errorFile, $errorLine) { if (isset(self::$_toStringException)) { $exception = self::$_toStringException; // Always unset '_toStringException', we don't want a straggler to be found later if something came between the setting and the error self::$_toStringException = null; if (preg_match('~^Method .*::__toString\(\) must return a string value$~', $errorMessage)) throw $exception; } return false; } public static function throwToStringException($exception) { // Should not occur with prescribed usage, but in case of recursion: clean out exception, return a valid string, and weep if (isset(self::$_toStringException)) { self::$_toStringException = null; return ''; } self::$_toStringException = $exception; return ''; } } class My_Class { public function doComplexStuff() { throw new Exception('Oh noes!'); } public function __toString() { try { // do your complex thing which might trigger an exception return $this->doComplexStuff(); } catch (Exception $e) { // The 'return' is required to trigger the trick return My_ToStringFixer::throwToStringException($e); } } } $x = new My_Class(); try { echo $x; } catch (Exception $e) { echo 'Caught Exception! : '. $e; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 12
Branch analysis from position: 12
2 jumps found. (Code = 107) Position 1 = 13, Position 2 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tCmE5
function name:  (null)
number of ops:  16
compiled vars:  !0 = $x, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'set_error_handler'
          1        SEND_VAL                                                 <array>
          2        DO_ICALL                                                 
    3     3        INIT_FCALL                                               'error_reporting'
          4        SEND_VAL                                                 32767
          5        DO_ICALL                                                 
   37     6        DECLARE_CLASS                                            'my_class'
   59     7        NEW                                              $4      'My_Class'
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $4
   63    10        ECHO                                                     !0
         11      > JMP                                                      ->15
   65    12  E > > CATCH                                       last         'Exception'
   67    13    >   CONCAT                                           ~7      'Caught+Exception%21+%3A+', !1
         14        ECHO                                                     ~7
   68    15    > > RETURN                                                   1

Class My_ToStringFixer:
Function errorhandler:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/tCmE5
function name:  errorHandler
number of ops:  18
compiled vars:  !0 = $errorNumber, !1 = $errorMessage, !2 = $errorFile, !3 = $errorLine, !4 = $exception
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   11     4        ISSET_ISEMPTY_STATIC_PROP                                '_toStringException'
          5      > JMPZ                                                     ~5, ->16
   13     6    >   FETCH_STATIC_PROP_R          unknown             ~6      '_toStringException'
          7        ASSIGN                                                   !4, ~6
   15     8        ASSIGN_STATIC_PROP                                       '_toStringException'
          9        OP_DATA                                                  null
   16    10        INIT_FCALL                                               'preg_match'
         11        SEND_VAL                                                 '%7E%5EMethod+.%2A%3A%3A__toString%5C%28%5C%29+must+return+a+string+value%24%7E'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $9      
         14      > JMPZ                                                     $9, ->16
   17    15    > > THROW                                         0          !4
   19    16    > > RETURN                                                   <false>
   20    17*     > RETURN                                                   null

End of function errorhandler

Function throwtostringexception:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tCmE5
function name:  throwToStringException
number of ops:  10
compiled vars:  !0 = $exception
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   25     1        ISSET_ISEMPTY_STATIC_PROP                                '_toStringException'
          2      > JMPZ                                                     ~1, ->6
   27     3    >   ASSIGN_STATIC_PROP                                       '_toStringException'
          4        OP_DATA                                                  null
   28     5      > RETURN                                                   ''
   31     6    >   ASSIGN_STATIC_PROP                                       '_toStringException'
          7        OP_DATA                                                  !0
   33     8      > RETURN                                                   ''
   34     9*     > RETURN                                                   null

End of function throwtostringexception

End of class My_ToStringFixer.

Class My_Class:
Function docomplexstuff:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/tCmE5
function name:  doComplexStuff
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   NEW                                              $0      'Exception'
          1        SEND_VAL_EX                                              'Oh+noes%21'
          2        DO_FCALL                                      0          
          3      > THROW                                         0          $0
   42     4*     > RETURN                                                   null

End of function docomplexstuff

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 5
Branch analysis from position: 5
2 jumps found. (Code = 107) Position 1 = 6, Position 2 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tCmE5
function name:  __toString
number of ops:  13
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   INIT_METHOD_CALL                                         'doComplexStuff'
          1        DO_FCALL                                      0  $1      
          2        VERIFY_RETURN_TYPE                                       $1
          3      > RETURN                                                   $1
          4*       JMP                                                      ->11
   51     5  E > > CATCH                                       last         'Exception'
   54     6    >   INIT_STATIC_METHOD_CALL                                  'My_ToStringFixer', 'throwToStringException'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $2      
          9        VERIFY_RETURN_TYPE                                       $2
         10      > RETURN                                                   $2
   56    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function __tostring

End of class My_Class.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
185.27 ms | 1404 KiB | 19 Q