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); }; 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; $source = file($filename); $body = implode("", array_slice($source, $start_line, $length)); $body = preg_replace('/(\$[a-z]+)\ \=\ function/', '\\$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]; } $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(" use (" . $vars . ")", "", $body); $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/89pXm
function name:  (null)
number of ops:  18
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                          ~9      [0]
          6        BIND_LEXICAL                                             ~9, !2
          7        BIND_LEXICAL                                             ~9, !1
          8        BIND_LEXICAL                                             ~9, !0
          9        ASSIGN                                                   !3, ~9
   13    10        INIT_FCALL_BY_NAME                                       'packAnonFunction'
         11        SEND_VAR_EX                                              !3
         12        SEND_VAR_EX                                              !2
         13        SEND_VAR_EX                                              !1
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0  $11     
         16        ECHO                                                     $11
   45    17      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/89pXm
function name:  {closure}
number of ops:  9
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      > RETURN                                                   null

End of Dynamic Function 0

Function packanonfunction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 100
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 58
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 77) Position 1 = 62, Position 2 = 68
Branch analysis from position: 62
2 jumps found. (Code = 78) Position 1 = 63, Position 2 = 68
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 68
2 jumps found. (Code = 77) Position 1 = 71, Position 2 = 84
Branch analysis from position: 71
2 jumps found. (Code = 78) Position 1 = 72, Position 2 = 84
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
Branch analysis from position: 84
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 84
Branch analysis from position: 68
Branch analysis from position: 58
2 jumps found. (Code = 77) Position 1 = 62, Position 2 = 68
Branch analysis from position: 62
Branch analysis from position: 68
Branch analysis from position: 100
filename:       /in/89pXm
function name:  packAnonFunction
number of ops:  102
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
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   15     2        NEW                                              $18     'ReflectionFunction'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $18
   16     6        INIT_METHOD_CALL                                         !2, 'getFileName'
          7        DO_FCALL                                      0  $21     
          8        ASSIGN                                                   !3, $21
   17     9        INIT_METHOD_CALL                                         !2, 'getStartLine'
         10        DO_FCALL                                      0  $23     
         11        SUB                                              ~24     $23, 1
         12        ASSIGN                                                   !4, ~24
   18    13        INIT_METHOD_CALL                                         !2, 'getEndLine'
         14        DO_FCALL                                      0  $26     
         15        ASSIGN                                                   !5, $26
   19    16        SUB                                              ~28     !5, !4
         17        ASSIGN                                                   !6, ~28
   21    18        INIT_FCALL                                               'file'
         19        SEND_VAR                                                 !3
         20        DO_ICALL                                         $30     
         21        ASSIGN                                                   !7, $30
   22    22        INIT_FCALL                                               'implode'
         23        SEND_VAL                                                 ''
         24        INIT_FCALL                                               'array_slice'
         25        SEND_VAR                                                 !7
         26        SEND_VAR                                                 !4
         27        SEND_VAR                                                 !6
         28        DO_ICALL                                         $32     
         29        SEND_VAR                                                 $32
         30        DO_ICALL                                         $33     
         31        ASSIGN                                                   !8, $33
   23    32        INIT_FCALL                                               'preg_replace'
         33        SEND_VAL                                                 '%2F%28%5C%24%5Ba-z%5D%2B%29%5C+%5C%3D%5C+function%2F'
         34        SEND_VAL                                                 '%5C%24payload+%3D+function'
         35        SEND_VAR                                                 !8
         36        DO_ICALL                                         $35     
         37        ASSIGN                                                   !8, $35
   24    38        INIT_FCALL                                               'preg_match'
         39        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'
         40        SEND_VAR                                                 !8
         41        SEND_REF                                                 !9
         42        DO_ICALL                                         $37     
         43      > JMPZ                                                     $37, ->100
   25    44    >   FETCH_DIM_R                                      ~38     !9, 1
         45        ASSIGN                                                   !10, ~38
   26    46        INIT_FCALL                                               'strpos'
         47        SEND_VAR                                                 !10
         48        SEND_VAL                                                 '%2C+'
         49        DO_ICALL                                         $40     
         50        TYPE_CHECK                                  1018          $40
         51      > JMPZ                                                     ~41, ->58
   27    52    >   INIT_FCALL                                               'explode'
         53        SEND_VAL                                                 '%2C+'
         54        SEND_VAR                                                 !10
         55        DO_ICALL                                         $42     
         56        ASSIGN                                                   !11, $42
   26    57      > JMP                                                      ->60
   29    58    >   INIT_ARRAY                                       ~44     !10
         59        ASSIGN                                                   !11, ~44
   31    60    >   ASSIGN                                                   !12, <array>
   32    61      > FE_RESET_R                                       $47     !11, ->68
         62    > > FE_FETCH_R                                       ~48     $47, !13, ->68
         63    >   ASSIGN                                                   !14, ~48
   33    64        FETCH_DIM_R                                      ~51     !1, !14
         65        ASSIGN_DIM                                               !12, !13
         66        OP_DATA                                                  ~51
   32    67      > JMP                                                      ->62
         68    >   FE_FREE                                                  $47
   35    69        ASSIGN                                                   !15, ''
   36    70      > FE_RESET_R                                       $53     !12, ->84
         71    > > FE_FETCH_R                                       ~54     $53, !16, ->84
         72    >   ASSIGN                                                   !17, ~54
   37    73        INIT_FCALL                                               'serialize'
         74        SEND_VAR                                                 !16
         75        DO_ICALL                                         $56     
         76        ASSIGN                                                   !16, $56
   38    77        ROPE_INIT                                     5  ~59     '%09'
         78        ROPE_ADD                                      1  ~59     ~59, !17
         79        ROPE_ADD                                      2  ~59     ~59, '+%3D+unserialize%28%27'
         80        ROPE_ADD                                      3  ~59     ~59, !16
         81        ROPE_END                                      4  ~58     ~59, '%27%29%3B%0A'
         82        ASSIGN_OP                                     8          !15, ~58
   36    83      > JMP                                                      ->71
         84    >   FE_FREE                                                  $53
   41    85        INIT_FCALL                                               'str_replace'
         86        CONCAT                                           ~63     '+use+%28', !10
         87        CONCAT                                           ~64     ~63, '%29'
         88        SEND_VAL                                                 ~64
         89        SEND_VAL                                                 ''
         90        SEND_VAR                                                 !8
         91        DO_ICALL                                         $65     
         92        ASSIGN                                                   !8, $65
   42    93        INIT_FCALL                                               'str_replace'
         94        SEND_VAL                                                 '%7B%0A'
         95        CONCAT                                           ~67     '%7B%0A', !15
         96        SEND_VAL                                                 ~67
         97        SEND_VAR                                                 !8
         98        DO_ICALL                                         $68     
         99        ASSIGN                                                   !8, $68
   44   100    > > RETURN                                                   !8
   45   101*     > RETURN                                                   null

End of function packanonfunction

Class TestClass: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.24 ms | 1033 KiB | 23 Q