3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TestClass { } $abc = "testABC"; $xyz = new TestClass(); $test = true; $x = function () use ($test, $xyz, $abc) { echo $abc; var_dump($test, $xyz); var_dump($this); }; $x->bindTo(new TestClass, "TestClass"); $x(); echo packAnonFunction($x, $test, $xyz, $abc); function packAnonFunction($payload, ...$args) { $func = new ReflectionFunction($payload); $filename = $func->getFileName(); $start_line = $func->getStartLine() - 1; $end_line = $func->getEndLine(); $length = $end_line - $start_line; var_dump($func->getClosureThis()); $source = file($filename); $body = implode("", array_slice($source, $start_line, $length)); $body = preg_replace('/(?:(?:(\$[a-z]+)\s\=)|return)\sfunction/', '\\$payload = function', $body); if(preg_match('/use\s\((\$[a-zA-Z0-9]+(?:,\s\$[a-zA-Z0-9]+)*)\)/', $body, $matches)) { $vars = $matches[1]; if(strpos($vars, ', ') !== false) { $parts = explode(', ', $vars); } else { $parts = [$vars]; } $body = str_replace(" use (" . $vars . ")", "", $body); } else { $parts = []; } if(strpos($body, '$this') !== false) { $parts[] = '$this'; $args[count($parts) - 1] = $func->getClosureThis(); } $return = []; foreach($parts as $key => $variable) { $return[$variable] = $args[$key]; } $variableString = ""; foreach($return as $var => $value) { $value = serialize($value); $variableString .= "\t{$var} = unserialize('{$value}');\n"; } $body = str_replace("{\n", "{\n" . $variableString, $body); return $body; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0RroH
function name:  (null)
number of ops:  26
compiled vars:  !0 = $abc, !1 = $xyz, !2 = $test, !3 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, 'testABC'
    6     1        NEW                                              $5      'TestClass'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $5
    7     4        ASSIGN                                                   !2, <true>
    8     5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F0RroH%3A8%240'
          6        BIND_LEXICAL                                             ~9, !2
          7        BIND_LEXICAL                                             ~9, !1
          8        BIND_LEXICAL                                             ~9, !0
          9        ASSIGN                                                   !3, ~9
   13    10        INIT_METHOD_CALL                                         !3, 'bindTo'
         11        NEW                                              $11     'TestClass'
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $11
         14        SEND_VAL_EX                                              'TestClass'
         15        DO_FCALL                                      0          
   15    16        INIT_DYNAMIC_CALL                                        !3
         17        DO_FCALL                                      0          
   17    18        INIT_FCALL_BY_NAME                                       'packAnonFunction'
         19        SEND_VAR_EX                                              !3
         20        SEND_VAR_EX                                              !2
         21        SEND_VAR_EX                                              !1
         22        SEND_VAR_EX                                              !0
         23        DO_FCALL                                      0  $15     
         24        ECHO                                                     $15
   56    25      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F0RroH%3A8%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0RroH
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $test, !1 = $xyz, !2 = $abc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
    9     3        ECHO                                                     !2
   10     4        INIT_FCALL                                               'var_dump'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
   11     8        INIT_FCALL                                               'var_dump'
          9        FETCH_THIS                                       ~4      
         10        SEND_VAL                                                 ~4
         11        DO_ICALL                                                 
   12    12      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F0RroH%3A8%240

Function packanonfunction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 74
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 63
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 89
Branch analysis from position: 81
2 jumps found. (Code = 77) Position 1 = 91, Position 2 = 97
Branch analysis from position: 91
2 jumps found. (Code = 78) Position 1 = 92, Position 2 = 97
Branch analysis from position: 92
1 jumps found. (Code = 42) Position 1 = 91
Branch analysis from position: 91
Branch analysis from position: 97
2 jumps found. (Code = 77) Position 1 = 100, Position 2 = 113
Branch analysis from position: 100
2 jumps found. (Code = 78) Position 1 = 101, Position 2 = 113
Branch analysis from position: 101
1 jumps found. (Code = 42) Position 1 = 100
Branch analysis from position: 100
Branch analysis from position: 113
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 113
Branch analysis from position: 97
Branch analysis from position: 89
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 74
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 89
Branch analysis from position: 81
Branch analysis from position: 89
filename:       /in/0RroH
function name:  packAnonFunction
number of ops:  123
compiled vars:  !0 = $payload, !1 = $args, !2 = $func, !3 = $filename, !4 = $start_line, !5 = $end_line, !6 = $length, !7 = $source, !8 = $body, !9 = $matches, !10 = $vars, !11 = $parts, !12 = $return, !13 = $variable, !14 = $key, !15 = $variableString, !16 = $value, !17 = $var
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   19     2        NEW                                              $18     'ReflectionFunction'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $18
   20     6        INIT_METHOD_CALL                                         !2, 'getFileName'
          7        DO_FCALL                                      0  $21     
          8        ASSIGN                                                   !3, $21
   21     9        INIT_METHOD_CALL                                         !2, 'getStartLine'
         10        DO_FCALL                                      0  $23     
         11        SUB                                              ~24     $23, 1
         12        ASSIGN                                                   !4, ~24
   22    13        INIT_METHOD_CALL                                         !2, 'getEndLine'
         14        DO_FCALL                                      0  $26     
         15        ASSIGN                                                   !5, $26
   23    16        SUB                                              ~28     !5, !4
         17        ASSIGN                                                   !6, ~28
   25    18        INIT_FCALL                                               'var_dump'
         19        INIT_METHOD_CALL                                         !2, 'getClosureThis'
         20        DO_FCALL                                      0  $30     
         21        SEND_VAR                                                 $30
         22        DO_ICALL                                                 
   27    23        INIT_FCALL                                               'file'
         24        SEND_VAR                                                 !3
         25        DO_ICALL                                         $32     
         26        ASSIGN                                                   !7, $32
   28    27        INIT_FCALL                                               'implode'
         28        SEND_VAL                                                 ''
         29        INIT_FCALL                                               'array_slice'
         30        SEND_VAR                                                 !7
         31        SEND_VAR                                                 !4
         32        SEND_VAR                                                 !6
         33        DO_ICALL                                         $34     
         34        SEND_VAR                                                 $34
         35        DO_ICALL                                         $35     
         36        ASSIGN                                                   !8, $35
   29    37        INIT_FCALL                                               'preg_replace'
         38        SEND_VAL                                                 '%2F%28%3F%3A%28%3F%3A%28%5C%24%5Ba-z%5D%2B%29%5Cs%5C%3D%29%7Creturn%29%5Csfunction%2F'
         39        SEND_VAL                                                 '%5C%24payload+%3D+function'
         40        SEND_VAR                                                 !8
         41        DO_ICALL                                         $37     
         42        ASSIGN                                                   !8, $37
   30    43        INIT_FCALL                                               'preg_match'
         44        SEND_VAL                                                 '%2Fuse%5Cs%5C%28%28%5C%24%5Ba-zA-Z0-9%5D%2B%28%3F%3A%2C%5Cs%5C%24%5Ba-zA-Z0-9%5D%2B%29%2A%29%5C%29%2F'
         45        SEND_VAR                                                 !8
         46        SEND_REF                                                 !9
         47        DO_ICALL                                         $39     
         48      > JMPZ                                                     $39, ->74
   31    49    >   FETCH_DIM_R                                      ~40     !9, 1
         50        ASSIGN                                                   !10, ~40
   32    51        INIT_FCALL                                               'strpos'
         52        SEND_VAR                                                 !10
         53        SEND_VAL                                                 '%2C+'
         54        DO_ICALL                                         $42     
         55        TYPE_CHECK                                  1018          $42
         56      > JMPZ                                                     ~43, ->63
   33    57    >   INIT_FCALL                                               'explode'
         58        SEND_VAL                                                 '%2C+'
         59        SEND_VAR                                                 !10
         60        DO_ICALL                                         $44     
         61        ASSIGN                                                   !11, $44
         62      > JMP                                                      ->65
   35    63    >   INIT_ARRAY                                       ~46     !10
         64        ASSIGN                                                   !11, ~46
   37    65    >   INIT_FCALL                                               'str_replace'
         66        CONCAT                                           ~48     '+use+%28', !10
         67        CONCAT                                           ~49     ~48, '%29'
         68        SEND_VAL                                                 ~49
         69        SEND_VAL                                                 ''
         70        SEND_VAR                                                 !8
         71        DO_ICALL                                         $50     
         72        ASSIGN                                                   !8, $50
         73      > JMP                                                      ->75
   39    74    >   ASSIGN                                                   !11, <array>
   41    75    >   INIT_FCALL                                               'strpos'
         76        SEND_VAR                                                 !8
         77        SEND_VAL                                                 '%24this'
         78        DO_ICALL                                         $53     
         79        TYPE_CHECK                                  1018          $53
         80      > JMPZ                                                     ~54, ->89
   42    81    >   ASSIGN_DIM                                               !11
         82        OP_DATA                                                  '%24this'
   43    83        COUNT                                            ~56     !11
         84        SUB                                              ~57     ~56, 1
         85        INIT_METHOD_CALL                                         !2, 'getClosureThis'
         86        DO_FCALL                                      0  $59     
         87        ASSIGN_DIM                                               !1, ~57
         88        OP_DATA                                                  $59
   45    89    >   ASSIGN                                                   !12, <array>
   46    90      > FE_RESET_R                                       $61     !11, ->97
         91    > > FE_FETCH_R                                       ~62     $61, !13, ->97
         92    >   ASSIGN                                                   !14, ~62
   47    93        FETCH_DIM_R                                      ~65     !1, !14
         94        ASSIGN_DIM                                               !12, !13
         95        OP_DATA                                                  ~65
   46    96      > JMP                                                      ->91
         97    >   FE_FREE                                                  $61
   49    98        ASSIGN                                                   !15, ''
   50    99      > FE_RESET_R                                       $67     !12, ->113
        100    > > FE_FETCH_R                                       ~68     $67, !16, ->113
        101    >   ASSIGN                                                   !17, ~68
   51   102        INIT_FCALL                                               'serialize'
        103        SEND_VAR                                                 !16
        104        DO_ICALL                                         $70     
        105        ASSIGN                                                   !16, $70
   52   106        ROPE_INIT                                     5  ~73     '%09'
        107        ROPE_ADD                                      1  ~73     ~73, !17
        108        ROPE_ADD                                      2  ~73     ~73, '+%3D+unserialize%28%27'
        109        ROPE_ADD                                      3  ~73     ~73, !16
        110        ROPE_END                                      4  ~72     ~73, '%27%29%3B%0A'
        111        ASSIGN_OP                                     8          !15, ~72
   50   112      > JMP                                                      ->100
        113    >   FE_FREE                                                  $67
   54   114        INIT_FCALL                                               'str_replace'
        115        SEND_VAL                                                 '%7B%0A'
        116        CONCAT                                           ~77     '%7B%0A', !15
        117        SEND_VAL                                                 ~77
        118        SEND_VAR                                                 !8
        119        DO_ICALL                                         $78     
        120        ASSIGN                                                   !8, $78
   55   121      > RETURN                                                   !8
   56   122*     > RETURN                                                   null

End of function packanonfunction

Class TestClass: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.15 ms | 1412 KiB | 33 Q