3v4l.org

run code in 300+ PHP versions simultaneously
<?php // If this would be for real, decorators would directly replace // the decorated functions/methods. function decorate(callable $fn, array $decorators) { foreach ($decorators as $d) { $wrapper = $d($fn); $fn = $wrapper; } return $wrapper; } function foo($bar) { throw new Exception("Error $bar!"); } function catch_all_exceptions(callable $fn) { return function() use ($fn) { try { return call_user_func_array($fn, func_get_args()); } catch (\Exception $e) { echo "catch_all_exceptions: Caught Exception: $e\n"; } }; } function once(callable $fn) { return function() use ($fn) { static $return; static $called = false; if (!$called) { $return = call_user_func_array($fn, func_get_args()); $called = true; } return $return; }; } function do_something_before(callable $fn) { return function() use ($fn) { echo "Before!\n"; return call_user_func_array($fn, func_get_args()); }; } $bar = decorate(function() { echo "Hello\n"; }, ["once"]); $bar(); $bar(); $bar(); $foo = decorate("foo", ["catch_all_exceptions", "do_something_before"]); var_dump($foo("bar"));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PQhSI
function name:  (null)
number of ops:  24
compiled vars:  !0 = $bar, !1 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   INIT_FCALL                                               'decorate'
          1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPQhSI%3A53%243'
          2        SEND_VAL                                                 ~2
          3        SEND_VAL                                                 <array>
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !0, $3
   54     6        INIT_DYNAMIC_CALL                                        !0
          7        DO_FCALL                                      0          
   55     8        INIT_DYNAMIC_CALL                                        !0
          9        DO_FCALL                                      0          
   56    10        INIT_DYNAMIC_CALL                                        !0
         11        DO_FCALL                                      0          
   58    12        INIT_FCALL                                               'decorate'
         13        SEND_VAL                                                 'foo'
         14        SEND_VAL                                                 <array>
         15        DO_FCALL                                      0  $8      
         16        ASSIGN                                                   !1, $8
   59    17        INIT_FCALL                                               'var_dump'
         18        INIT_DYNAMIC_CALL                                        !1
         19        SEND_VAL_EX                                              'bar'
         20        DO_FCALL                                      0  $10     
         21        SEND_VAR                                                 $10
         22        DO_ICALL                                                 
         23      > RETURN                                                   1

Function decorate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/PQhSI
function name:  decorate
number of ops:  13
compiled vars:  !0 = $fn, !1 = $decorators, !2 = $d, !3 = $wrapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2      > FE_RESET_R                                       $4      !1, ->10
          3    > > FE_FETCH_R                                               $4, !2, ->10
    8     4    >   INIT_DYNAMIC_CALL                                        !2
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !3, $5
    9     8        ASSIGN                                                   !0, !3
    7     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $4
   12    11      > RETURN                                                   !3
   13    12*     > RETURN                                                   null

End of function decorate

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/PQhSI
function name:  foo
number of ops:  9
compiled vars:  !0 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     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
   18     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/PQhSI
function name:  catch_all_exceptions
number of ops:  5
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   22     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPQhSI%3A22%240'
          2        BIND_LEXICAL                                             ~1, !0
   28     3      > RETURN                                                   ~1
   29     4*     > RETURN                                                   null

End of function catch_all_exceptions

Function %00%7Bclosure%7D%2Fin%2FPQhSI%3A22%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 8
Branch analysis from position: 8
2 jumps found. (Code = 107) Position 1 = 9, Position 2 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PQhSI
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $fn, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   BIND_STATIC                                              !0
   24     1        INIT_USER_CALL                                0          'call_user_func_array', !0
          2        FUNC_GET_ARGS                                    ~2      
          3        SEND_ARRAY                                               ~2
          4        CHECK_UNDEF_ARGS                                         
          5        DO_FCALL                                      0  $3      
          6      > RETURN                                                   $3
          7*       JMP                                                      ->13
   25     8  E > > CATCH                                       last         'Exception'
   26     9    >   ROPE_INIT                                     3  ~5      'catch_all_exceptions%3A+Caught+Exception%3A+'
         10        ROPE_ADD                                      1  ~5      ~5, !1
         11        ROPE_END                                      2  ~4      ~5, '%0A'
         12        ECHO                                                     ~4
   28    13      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPQhSI%3A22%240

Function once:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PQhSI
function name:  once
number of ops:  5
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPQhSI%3A32%241'
          2        BIND_LEXICAL                                             ~1, !0
   42     3      > RETURN                                                   ~1
   43     4*     > RETURN                                                   null

End of function once

Function %00%7Bclosure%7D%2Fin%2FPQhSI%3A32%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/PQhSI
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $fn, !1 = $return, !2 = $called
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   BIND_STATIC                                              !0
   33     1        BIND_STATIC                                              !1
   34     2        BIND_STATIC                                              !2
   36     3        BOOL_NOT                                         ~3      !2
          4      > JMPZ                                                     ~3, ->12
   37     5    >   INIT_USER_CALL                                0          'call_user_func_array', !0
          6        FUNC_GET_ARGS                                    ~4      
          7        SEND_ARRAY                                               ~4
          8        CHECK_UNDEF_ARGS                                         
          9        DO_FCALL                                      0  $5      
         10        ASSIGN                                                   !1, $5
   38    11        ASSIGN                                                   !2, <true>
   41    12    > > RETURN                                                   !1
   42    13*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPQhSI%3A32%241

Function do_something_before:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PQhSI
function name:  do_something_before
number of ops:  5
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   47     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPQhSI%3A47%242'
          2        BIND_LEXICAL                                             ~1, !0
   50     3      > RETURN                                                   ~1
   51     4*     > RETURN                                                   null

End of function do_something_before

Function %00%7Bclosure%7D%2Fin%2FPQhSI%3A47%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PQhSI
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   BIND_STATIC                                              !0
   48     1        ECHO                                                     'Before%21%0A'
   49     2        INIT_USER_CALL                                0          'call_user_func_array', !0
          3        FUNC_GET_ARGS                                    ~1      
          4        SEND_ARRAY                                               ~1
          5        CHECK_UNDEF_ARGS                                         
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   50     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPQhSI%3A47%242

Function %00%7Bclosure%7D%2Fin%2FPQhSI%3A53%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PQhSI
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   ECHO                                                     'Hello%0A'
          1      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPQhSI%3A53%243

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
258.92 ms | 1411 KiB | 18 Q