3v4l.org

run code in 300+ PHP versions simultaneously
<?php function decorate(array $decorators, callable $fn, array $arguments = array()) { foreach (array_reverse($decorators) as $decorator) { if (is_callable($decorator)) { $fn = $decorator($fn); } } return call_user_func_array($fn, $arguments); } function decorator(callable $decorator) { // This function replaces 'trace' return function($wrapped) use ($decorator) { // This function replaces the function which 'trace' is used on. return function() use ($wrapped, $decorator) { var_dump($wrapped); $arguments = func_get_args(); return call_user_func($decorator, $wrapped, $arguments); }; }; } function expects(/* $type,... */) { $types = func_get_args(); $decorator = function(callable $fn) use ($types) { $wrapper = function() use ($fn, $types) { foreach (func_get_args() as $i => $arg) { if (($type = gettype($arg)) !== $types[$i] and isset($types[$i]) and $types[$i] !== 'mixed') { throw new \InvalidArgumentException("Expected type of argument $i to be {$types[$i]}, got $type"); } } return call_user_func_array($fn, func_get_args()); }; return $wrapper; }; return $decorator; } function returns($type) { $decorator = function($fn) use ($type) { $wrapper = function() use ($fn, $type) { $returnValue = call_user_func_array($fn, func_get_args()); if (($returnType = gettype($returnValue)) !== $type) { throw new \UnexpectedValueException("Expected return type $type, got $returnType"); } return $returnValue; }; return $wrapper; }; return $decorator; } function trace(callable $fn, array $arguments) { return decorate( ['decorator'], function($fn, $arguments) { printf("Calling %s with arguments %s\n", print_r($fn, true), print_r($arguments, true)); return call_user_func_array($fn, $arguments); }, func_get_args() ); } function foo() { return decorate(['trace'], function() { echo "Hello World\n"; }, func_get_args()); } function bar() { return decorate( [returns('string')], function() { return "foo"; }, func_get_args() ); } foo(); bar();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  (null)
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   96     0  E >   INIT_FCALL                                               'foo'
          1        DO_FCALL                                      0          
   97     2        INIT_FCALL                                               'bar'
          3        DO_FCALL                                      0          
          4      > RETURN                                                   1

Function decorate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/jASTJ
function name:  decorate
number of ops:  24
compiled vars:  !0 = $decorators, !1 = $fn, !2 = $arguments, !3 = $decorator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
    5     3        INIT_FCALL                                               'array_reverse'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $4      
          6      > FE_RESET_R                                       $5      $4, ->17
          7    > > FE_FETCH_R                                               $5, !3, ->17
    6     8    >   INIT_FCALL                                               'is_callable'
          9        SEND_VAR                                                 !3
         10        DO_ICALL                                         $6      
         11      > JMPZ                                                     $6, ->16
    7    12    >   INIT_DYNAMIC_CALL                                        !3
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $7      
         15        ASSIGN                                                   !1, $7
    5    16    > > JMP                                                      ->7
         17    >   FE_FREE                                                  $5
   11    18        INIT_USER_CALL                                0          'call_user_func_array', !1
         19        SEND_ARRAY                                               !2
         20        CHECK_UNDEF_ARGS                                         
         21        DO_FCALL                                      0  $9      
         22      > RETURN                                                   $9
   12    23*     > RETURN                                                   null

End of function decorate

Function decorator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  decorator
number of ops:  5
compiled vars:  !0 = $decorator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   17     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A17%240'
          2        BIND_LEXICAL                                             ~1, !0
   24     3      > RETURN                                                   ~1
   25     4*     > RETURN                                                   null

End of function decorator

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A17%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $wrapped, !1 = $decorator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   19     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A19%241'
          3        BIND_LEXICAL                                             ~2, !0
          4        BIND_LEXICAL                                             ~2, !1
   23     5      > RETURN                                                   ~2
   24     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A17%240

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A19%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $wrapped, !1 = $decorator, !2 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   20     2        INIT_FCALL                                               'var_dump'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                                 
   21     5        FUNC_GET_ARGS                                    ~4      
          6        ASSIGN                                                   !2, ~4
   22     7        INIT_USER_CALL                                2          'call_user_func', !1
          8        SEND_USER                                                !0
          9        SEND_USER                                                !2
         10        DO_FCALL                                      0  $6      
         11      > RETURN                                                   $6
   23    12*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A19%241

Function expects:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  expects
number of ops:  7
compiled vars:  !0 = $types, !1 = $decorator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   FUNC_GET_ARGS                                    ~2      
          1        ASSIGN                                                   !0, ~2
   31     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A31%242'
          3        BIND_LEXICAL                                             ~4, !0
          4        ASSIGN                                                   !1, ~4
   45     5      > RETURN                                                   !1
   46     6*     > RETURN                                                   null

End of function expects

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A31%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $fn, !1 = $types, !2 = $wrapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   32     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A32%243'
          3        BIND_LEXICAL                                             ~3, !0
          4        BIND_LEXICAL                                             ~3, !1
          5        ASSIGN                                                   !2, ~3
   42     6      > RETURN                                                   !2
   43     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A31%242

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A32%243:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 30
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 30
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 29
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 17
Branch analysis from position: 13
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/jASTJ
function name:  {closure}
number of ops:  38
compiled vars:  !0 = $fn, !1 = $types, !2 = $arg, !3 = $i, !4 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   33     2        FUNC_GET_ARGS                                    ~5      
          3      > FE_RESET_R                                       $6      ~5, ->30
          4    > > FE_FETCH_R                                       ~7      $6, !2, ->30
          5    >   ASSIGN                                                   !3, ~7
   34     6        GET_TYPE                                         ~9      !2
          7        ASSIGN                                           ~10     !4, ~9
          8        FETCH_DIM_R                                      ~11     !1, !3
          9        IS_NOT_IDENTICAL                                 ~12     ~10, ~11
         10      > JMPZ_EX                                          ~12     ~12, ->13
         11    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~13     !1, !3
         12        BOOL                                             ~12     ~13
         13    > > JMPZ_EX                                          ~12     ~12, ->17
         14    >   FETCH_DIM_R                                      ~14     !1, !3
         15        IS_NOT_IDENTICAL                                 ~15     ~14, 'mixed'
         16        BOOL                                             ~12     ~15
         17    > > JMPZ                                                     ~12, ->29
   35    18    >   NEW                                              $16     'InvalidArgumentException'
         19        ROPE_INIT                                     6  ~19     'Expected+type+of+argument+'
         20        ROPE_ADD                                      1  ~19     ~19, !3
         21        ROPE_ADD                                      2  ~19     ~19, '+to+be+'
         22        FETCH_DIM_R                                      ~17     !1, !3
         23        ROPE_ADD                                      3  ~19     ~19, ~17
         24        ROPE_ADD                                      4  ~19     ~19, '%2C+got+'
         25        ROPE_END                                      5  ~18     ~19, !4
         26        SEND_VAL_EX                                              ~18
         27        DO_FCALL                                      0          
         28      > THROW                                         0          $16
   33    29    > > JMP                                                      ->4
         30    >   FE_FREE                                                  $6
   39    31        INIT_USER_CALL                                0          'call_user_func_array', !0
         32        FUNC_GET_ARGS                                    ~23     
         33        SEND_ARRAY                                               ~23
         34        CHECK_UNDEF_ARGS                                         
         35        DO_FCALL                                      0  $24     
         36      > RETURN                                                   $24
   40    37*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A32%243

Function returns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  returns
number of ops:  6
compiled vars:  !0 = $type, !1 = $decorator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   50     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A50%244'
          2        BIND_LEXICAL                                             ~2, !0
          3        ASSIGN                                                   !1, ~2
   64     4      > RETURN                                                   !1
   65     5*     > RETURN                                                   null

End of function returns

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A50%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $fn, !1 = $type, !2 = $wrapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   51     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A51%245'
          3        BIND_LEXICAL                                             ~3, !0
          4        BIND_LEXICAL                                             ~3, !1
          5        ASSIGN                                                   !2, ~3
   61     6      > RETURN                                                   !2
   62     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A50%244

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A51%245:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  22
compiled vars:  !0 = $fn, !1 = $type, !2 = $returnValue, !3 = $returnType
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   52     2        INIT_USER_CALL                                0          'call_user_func_array', !0
          3        FUNC_GET_ARGS                                    ~4      
          4        SEND_ARRAY                                               ~4
          5        CHECK_UNDEF_ARGS                                         
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !2, $5
   54     8        GET_TYPE                                         ~7      !2
          9        ASSIGN                                           ~8      !3, ~7
         10        IS_NOT_IDENTICAL                                         !1, ~8
         11      > JMPZ                                                     ~9, ->20
   55    12    >   NEW                                              $10     'UnexpectedValueException'
         13        ROPE_INIT                                     4  ~12     'Expected+return+type+'
         14        ROPE_ADD                                      1  ~12     ~12, !1
         15        ROPE_ADD                                      2  ~12     ~12, '%2C+got+'
         16        ROPE_END                                      3  ~11     ~12, !3
         17        SEND_VAL_EX                                              ~11
         18        DO_FCALL                                      0          
         19      > THROW                                         0          $10
   58    20    > > RETURN                                                   !2
   59    21*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A51%245

Function trace:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  trace
number of ops:  11
compiled vars:  !0 = $fn, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   68     2        INIT_FCALL                                               'decorate'
   69     3        SEND_VAL                                                 <array>
   70     4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A70%246'
   73     5        SEND_VAL                                                 ~2
   74     6        FUNC_GET_ARGS                                    ~3      
          7        SEND_VAL                                                 ~3
          8        DO_FCALL                                      0  $4      
          9      > RETURN                                                   $4
   76    10*     > RETURN                                                   null

End of function trace

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A70%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  21
compiled vars:  !0 = $fn, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   71     2        INIT_FCALL                                               'printf'
          3        SEND_VAL                                                 'Calling+%25s+with+arguments+%25s%0A'
          4        INIT_FCALL                                               'print_r'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $2      
          8        SEND_VAR                                                 $2
          9        INIT_FCALL                                               'print_r'
         10        SEND_VAR                                                 !1
         11        SEND_VAL                                                 <true>
         12        DO_ICALL                                         $3      
         13        SEND_VAR                                                 $3
         14        DO_ICALL                                                 
   72    15        INIT_USER_CALL                                0          'call_user_func_array', !0
         16        SEND_ARRAY                                               !1
         17        CHECK_UNDEF_ARGS                                         
         18        DO_FCALL                                      0  $5      
         19      > RETURN                                                   $5
   73    20*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A70%246

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  foo
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   INIT_FCALL                                               'decorate'
          1        SEND_VAL                                                 <array>
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A80%247'
   82     3        SEND_VAL                                                 ~0
          4        FUNC_GET_ARGS                                    ~1      
          5        SEND_VAL                                                 ~1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   83     8*     > RETURN                                                   null

End of function foo

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

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A80%247

Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  bar
number of ops:  13
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   87     0  E >   INIT_FCALL                                               'decorate'
   88     1        INIT_FCALL                                               'returns'
          2        SEND_VAL                                                 'string'
          3        DO_FCALL                                      0  $0      
          4        INIT_ARRAY                                       ~1      $0
          5        SEND_VAL                                                 ~1
   89     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FjASTJ%3A89%248'
   91     7        SEND_VAL                                                 ~2
   92     8        FUNC_GET_ARGS                                    ~3      
          9        SEND_VAL                                                 ~3
         10        DO_FCALL                                      0  $4      
         11      > RETURN                                                   $4
   94    12*     > RETURN                                                   null

End of function bar

Function %00%7Bclosure%7D%2Fin%2FjASTJ%3A89%248:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jASTJ
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E > > RETURN                                                   'foo'
   91     1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FjASTJ%3A89%248

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.01 ms | 1423 KiB | 29 Q