3v4l.org

run code in 300+ PHP versions simultaneously
<?php function closure_to_str($func) { $refl = new \ReflectionFunction($func); // get reflection object $path = $refl->getFileName(); // absolute path of php file $begn = $refl->getStartLine(); // have to `-1` for array index $endn = $refl->getEndLine(); $dlim = "\n"; // or PHP_EOL $list = explode($dlim, file_get_contents($path)); // lines of php-file source $list = array_slice($list, ($begn-1), ($endn-($begn-1))); // lines of closure definition $last = (count($list)-1); // last line number if((substr_count($list[0],'function')>1)|| (substr_count($list[0],'{')>1) || (substr_count($list[$last],'}')>1)) { throw new \Exception("Too complex context definition in: `$path`. Check lines: $begn & $endn."); } $list[0] = ('function'.explode('function',$list[0])[1]); $list[$last] = (explode('}',$list[$last])[0].'}'); return implode($list,$dlim); } $dog2 = ['bark'=>function($foo = '{'){}]; echo closure_to_str($dog2['bark'])."\n\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CEmqV
function name:  (null)
number of ops:  10
compiled vars:  !0 = $dog2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FCEmqV%3A23%240'
          1        INIT_ARRAY                                       ~2      ~1, 'bark'
          2        ASSIGN                                                   !0, ~2
   25     3        INIT_FCALL                                               'closure_to_str'
          4        FETCH_DIM_R                                      ~4      !0, 'bark'
          5        SEND_VAL                                                 ~4
          6        DO_FCALL                                      0  $5      
          7        CONCAT                                           ~6      $5, '%0A%0A'
          8        ECHO                                                     ~6
          9      > RETURN                                                   1

Function closure_to_str:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 42, Position 2 = 49
Branch analysis from position: 42
2 jumps found. (Code = 47) Position 1 = 50, Position 2 = 57
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 69
Branch analysis from position: 58
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 57
Branch analysis from position: 49
filename:       /in/CEmqV
function name:  closure_to_str
number of ops:  93
compiled vars:  !0 = $func, !1 = $refl, !2 = $path, !3 = $begn, !4 = $endn, !5 = $dlim, !6 = $list, !7 = $last
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        NEW                                              $8      'ReflectionFunction'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $8
    6     5        INIT_METHOD_CALL                                         !1, 'getFileName'
          6        DO_FCALL                                      0  $11     
          7        ASSIGN                                                   !2, $11
    7     8        INIT_METHOD_CALL                                         !1, 'getStartLine'
          9        DO_FCALL                                      0  $13     
         10        ASSIGN                                                   !3, $13
    8    11        INIT_METHOD_CALL                                         !1, 'getEndLine'
         12        DO_FCALL                                      0  $15     
         13        ASSIGN                                                   !4, $15
    9    14        ASSIGN                                                   !5, '%0A'
   10    15        INIT_FCALL                                               'explode'
         16        SEND_VAR                                                 !5
         17        INIT_FCALL                                               'file_get_contents'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $18     
         20        SEND_VAR                                                 $18
         21        DO_ICALL                                         $19     
         22        ASSIGN                                                   !6, $19
   11    23        INIT_FCALL                                               'array_slice'
         24        SEND_VAR                                                 !6
         25        SUB                                              ~21     !3, 1
         26        SEND_VAL                                                 ~21
         27        SUB                                              ~22     !3, 1
         28        SUB                                              ~23     !4, ~22
         29        SEND_VAL                                                 ~23
         30        DO_ICALL                                         $24     
         31        ASSIGN                                                   !6, $24
   12    32        COUNT                                            ~26     !6
         33        SUB                                              ~27     ~26, 1
         34        ASSIGN                                                   !7, ~27
   14    35        INIT_FCALL                                               'substr_count'
         36        FETCH_DIM_R                                      ~29     !6, 0
         37        SEND_VAL                                                 ~29
         38        SEND_VAL                                                 'function'
         39        DO_ICALL                                         $30     
         40        IS_SMALLER                                       ~31     1, $30
         41      > JMPNZ_EX                                         ~31     ~31, ->49
         42    >   INIT_FCALL                                               'substr_count'
         43        FETCH_DIM_R                                      ~32     !6, 0
         44        SEND_VAL                                                 ~32
         45        SEND_VAL                                                 '%7B'
         46        DO_ICALL                                         $33     
         47        IS_SMALLER                                       ~34     1, $33
         48        BOOL                                             ~31     ~34
         49    > > JMPNZ_EX                                         ~31     ~31, ->57
         50    >   INIT_FCALL                                               'substr_count'
         51        FETCH_DIM_R                                      ~35     !6, !7
         52        SEND_VAL                                                 ~35
         53        SEND_VAL                                                 '%7D'
         54        DO_ICALL                                         $36     
         55        IS_SMALLER                                       ~37     1, $36
         56        BOOL                                             ~31     ~37
         57    > > JMPZ                                                     ~31, ->69
   15    58    >   NEW                                              $38     'Exception'
         59        ROPE_INIT                                     7  ~40     'Too+complex+context+definition+in%3A+%60'
         60        ROPE_ADD                                      1  ~40     ~40, !2
         61        ROPE_ADD                                      2  ~40     ~40, '%60.+Check+lines%3A+'
         62        ROPE_ADD                                      3  ~40     ~40, !3
         63        ROPE_ADD                                      4  ~40     ~40, '+%26+'
         64        ROPE_ADD                                      5  ~40     ~40, !4
         65        ROPE_END                                      6  ~39     ~40, '.'
         66        SEND_VAL_EX                                              ~39
         67        DO_FCALL                                      0          
         68      > THROW                                         0          $38
   17    69    >   INIT_FCALL                                               'explode'
         70        SEND_VAL                                                 'function'
         71        FETCH_DIM_R                                      ~46     !6, 0
         72        SEND_VAL                                                 ~46
         73        DO_ICALL                                         $47     
         74        FETCH_DIM_R                                      ~48     $47, 1
         75        CONCAT                                           ~49     'function', ~48
         76        ASSIGN_DIM                                               !6, 0
         77        OP_DATA                                                  ~49
   18    78        INIT_FCALL                                               'explode'
         79        SEND_VAL                                                 '%7D'
         80        FETCH_DIM_R                                      ~51     !6, !7
         81        SEND_VAL                                                 ~51
         82        DO_ICALL                                         $52     
         83        FETCH_DIM_R                                      ~53     $52, 0
         84        CONCAT                                           ~54     ~53, '%7D'
         85        ASSIGN_DIM                                               !6, !7
         86        OP_DATA                                                  ~54
   20    87        INIT_FCALL                                               'implode'
         88        SEND_VAR                                                 !6
         89        SEND_VAR                                                 !5
         90        DO_ICALL                                         $55     
         91      > RETURN                                                   $55
   21    92*     > RETURN                                                   null

End of function closure_to_str

Function %00%7Bclosure%7D%2Fin%2FCEmqV%3A23%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CEmqV
function name:  {closure}
number of ops:  2
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV_INIT                                        !0      '%7B'
          1      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FCEmqV%3A23%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.86 ms | 1411 KiB | 24 Q