3v4l.org

run code in 300+ PHP versions simultaneously
<?php function decorate(callable $fn, callable $decorator) { $wrapper = $decorator(); return function() use ($fn, $wrapper) { $gen = $wrapper(); // Only call the wrapped function if the decorator made "yield" if (!$gen instanceof \Generator) { return; } // Use the yielded arguments, forward all if none given. if (is_array($gen->current())) { $arguments = $gen->current(); } else { $arguments = func_get_args(); } // Forward all throwed Exceptions try { // Send the decorated function's return value into the // decorator as return value of "yield" $gen->send(call_user_func_array($fn, $arguments)); } catch (\Exception $e) { $gen->throw($e); } }; } function foo($bar) { throw new Exception("Error $bar!"); } function catch_all_exceptions() { return function() { try { (yield); } catch (\Exception $e) { echo "catch_all_exceptions: Caught Exception: $e\n"; } }; } function do_something_before() { return function() { echo "Before!\n"; yield; }; } $foo = decorate(decorate("foo", "catch_all_exceptions"), "do_something_before"); $foo("bar");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DJQ41
function name:  (null)
number of ops:  13
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   INIT_FCALL                                               'decorate'
          1        INIT_FCALL                                               'decorate'
          2        SEND_VAL                                                 'foo'
          3        SEND_VAL                                                 'catch_all_exceptions'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR                                                 $1
          6        SEND_VAL                                                 'do_something_before'
          7        DO_FCALL                                      0  $2      
          8        ASSIGN                                                   !0, $2
   58     9        INIT_DYNAMIC_CALL                                        !0
         10        SEND_VAL_EX                                              'bar'
         11        DO_FCALL                                      0          
         12      > RETURN                                                   1

Function decorate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DJQ41
function name:  decorate
number of ops:  10
compiled vars:  !0 = $fn, !1 = $decorator, !2 = $wrapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        INIT_DYNAMIC_CALL                                        !1
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !2, $3
    7     5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDJQ41%3A7%240'
          6        BIND_LEXICAL                                             ~5, !0
          7        BIND_LEXICAL                                             ~5, !2
   30     8      > RETURN                                                   ~5
   31     9*     > RETURN                                                   null

End of function decorate

Function %00%7Bclosure%7D%2Fin%2FDJQ41%3A7%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Found catch point at position: 27
Branch analysis from position: 27
2 jumps found. (Code = 107) Position 1 = 28, Position 2 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DJQ41
function name:  {closure}
number of ops:  32
compiled vars:  !0 = $fn, !1 = $wrapper, !2 = $gen, !3 = $arguments, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
    8     2        INIT_DYNAMIC_CALL                                        !1
          3        DO_FCALL                                      0  $5      
          4        ASSIGN                                                   !2, $5
   11     5        INSTANCEOF                                       ~7      !2, 'Generator'
          6        BOOL_NOT                                         ~8      ~7
          7      > JMPZ                                                     ~8, ->9
   12     8    > > RETURN                                                   null
   16     9    >   INIT_METHOD_CALL                                         !2, 'current'
         10        DO_FCALL                                      0  $9      
         11        TYPE_CHECK                                  128          $9
         12      > JMPZ                                                     ~10, ->17
   17    13    >   INIT_METHOD_CALL                                         !2, 'current'
         14        DO_FCALL                                      0  $11     
         15        ASSIGN                                                   !3, $11
         16      > JMP                                                      ->19
   19    17    >   FUNC_GET_ARGS                                    ~13     
         18        ASSIGN                                                   !3, ~13
   26    19    >   INIT_METHOD_CALL                                         !2, 'send'
         20        INIT_USER_CALL                                0          'call_user_func_array', !0
         21        SEND_ARRAY                                               !3
         22        CHECK_UNDEF_ARGS                                         
         23        DO_FCALL                                      0  $15     
         24        SEND_VAR_NO_REF_EX                                       $15
         25        DO_FCALL                                      0          
         26      > JMP                                                      ->31
   27    27  E > > CATCH                                       last         'Exception'
   28    28    >   INIT_METHOD_CALL                                         !2, 'throw'
         29        SEND_VAR_EX                                              !4
         30        DO_FCALL                                      0          
   30    31    > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDJQ41%3A7%240

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/DJQ41
function name:  foo
number of ops:  9
compiled vars:  !0 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        NEW                                              $1      'Exception'
          2        ROPE_INIT                                     3  ~3      'Error+'
          3        ROPE_ADD                                      1  ~3      ~3, !0
          4        ROPE_END                                      2  ~2      ~3, '%21'
          5        SEND_VAL_EX                                              ~2
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $1
   36     8*     > RETURN                                                   null

End of function foo

Function catch_all_exceptions:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DJQ41
function name:  catch_all_exceptions
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDJQ41%3A40%241'
   46     1      > RETURN                                                   ~0
   47     2*     > RETURN                                                   null

End of function catch_all_exceptions

Function %00%7Bclosure%7D%2Fin%2FDJQ41%3A40%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
1 jumps found. (Code = 161) Position 1 = -2
Found catch point at position: 3
Branch analysis from position: 3
2 jumps found. (Code = 107) Position 1 = 4, Position 2 = -2
Branch analysis from position: 4
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/DJQ41
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   GENERATOR_CREATE                                         
   42     1        YIELD                                                    
          2      > JMP                                                      ->8
   43     3  E > > CATCH                                       last         'Exception'
   44     4    >   ROPE_INIT                                     3  ~3      'catch_all_exceptions%3A+Caught+Exception%3A+'
          5        ROPE_ADD                                      1  ~3      ~3, !0
          6        ROPE_END                                      2  ~2      ~3, '%0A'
          7        ECHO                                                     ~2
   46     8    > > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2FDJQ41%3A40%241

Function do_something_before:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DJQ41
function name:  do_something_before
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDJQ41%3A51%242'
   54     1      > RETURN                                                   ~0
   55     2*     > RETURN                                                   null

End of function do_something_before

Function %00%7Bclosure%7D%2Fin%2FDJQ41%3A51%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/DJQ41
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   GENERATOR_CREATE                                         
   52     1        ECHO                                                     'Before%21%0A'
   53     2        YIELD                                                    
   54     3      > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2FDJQ41%3A51%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.48 ms | 1406 KiB | 15 Q