3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ClosureHash { /** * List of hashes * * @var SplObjectStorage */ protected static $hashes = null; /** * Returns a hash for closure * * @param callable $closure * * @return string */ public static function from(Closure $closure) { if (!self::$hashes) { self::$hashes = new SplObjectStorage(); } if (!isset(self::$hashes[$closure])) { $ref = new ReflectionFunction($closure); $file = new SplFileObject($ref->getFileName()); $file->seek($ref->getStartLine()-1); $content = ''; while ($file->key() < $ref->getEndLine()) { $content .= $file->current(); $file->next(); } self::$hashes[$closure] = md5(json_encode(array( $content, $ref->getStaticVariables() ))); } return self::$hashes[$closure]; } } class Test { public function hello($greeting) { $closure = function ($message) use ($greeting, &$closure) { echo "Inside: ", ClosureHash::from($closure), PHP_EOL, "<br>" ; }; return $closure; } } $obj = new Test(); $closure = $obj->hello('Hello'); $closure('PHP'); echo "Outside: ", ClosureHash::from($closure), PHP_EOL, "<br>"; $another = $obj->hello('Bonjour'); $another('PHP'); echo "Outside: ", ClosureHash::from($another), PHP_EOL, "<br>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IDT6m
function name:  (null)
number of ops:  32
compiled vars:  !0 = $obj, !1 = $closure, !2 = $another
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   NEW                                              $3      'Test'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   55     3        INIT_METHOD_CALL                                         !0, 'hello'
          4        SEND_VAL_EX                                              'Hello'
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !1, $6
   56     7        INIT_DYNAMIC_CALL                                        !1
          8        SEND_VAL_EX                                              'PHP'
          9        DO_FCALL                                      0          
   57    10        ECHO                                                     'Outside%3A+'
         11        INIT_STATIC_METHOD_CALL                                  'ClosureHash', 'from'
         12        SEND_VAR                                                 !1
         13        DO_FCALL                                      0  $9      
         14        ECHO                                                     $9
         15        ECHO                                                     '%0A'
         16        ECHO                                                     '%3Cbr%3E'
   59    17        INIT_METHOD_CALL                                         !0, 'hello'
         18        SEND_VAL_EX                                              'Bonjour'
         19        DO_FCALL                                      0  $10     
         20        ASSIGN                                                   !2, $10
   60    21        INIT_DYNAMIC_CALL                                        !2
         22        SEND_VAL_EX                                              'PHP'
         23        DO_FCALL                                      0          
   61    24        ECHO                                                     'Outside%3A+'
         25        INIT_STATIC_METHOD_CALL                                  'ClosureHash', 'from'
         26        SEND_VAR                                                 !2
         27        DO_FCALL                                      0  $13     
         28        ECHO                                                     $13
         29        ECHO                                                     '%0A'
         30        ECHO                                                     '%3Cbr%3E'
         31      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FIDT6m%3A46%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IDT6m
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $message, !1 = $greeting, !2 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   47     3        ECHO                                                     'Inside%3A+'
          4        INIT_STATIC_METHOD_CALL                                  'ClosureHash', 'from'
          5        SEND_VAR                                                 !2
          6        DO_FCALL                                      0  $3      
          7        ECHO                                                     $3
          8        ECHO                                                     '%0A'
          9        ECHO                                                     '%3Cbr%3E'
   48    10      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FIDT6m%3A46%240

Class ClosureHash:
Function from:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 54
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 30
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 30
Branch analysis from position: 41
Branch analysis from position: 30
Branch analysis from position: 54
Branch analysis from position: 8
filename:       /in/IDT6m
function name:  from
number of ops:  58
compiled vars:  !0 = $closure, !1 = $ref, !2 = $file, !3 = $content
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   20     1        FETCH_STATIC_PROP_R          global lock         ~4      'hashes'
          2        BOOL_NOT                                         ~5      ~4
          3      > JMPZ                                                     ~5, ->8
   21     4    >   NEW                                              $7      'SplObjectStorage'
          5        DO_FCALL                                      0          
          6        ASSIGN_STATIC_PROP                                       'hashes'
          7        OP_DATA                                                  $7
   24     8    >   FETCH_STATIC_PROP_IS                             ~9      'hashes'
          9        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     ~9, !0
         10        BOOL_NOT                                         ~11     ~10
         11      > JMPZ                                                     ~11, ->54
   25    12    >   NEW                                              $12     'ReflectionFunction'
         13        SEND_VAR_EX                                              !0
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !1, $12
   26    16        NEW                                              $15     'SplFileObject'
         17        INIT_METHOD_CALL                                         !1, 'getFileName'
         18        DO_FCALL                                      0  $16     
         19        SEND_VAR_NO_REF_EX                                       $16
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !2, $15
   27    22        INIT_METHOD_CALL                                         !2, 'seek'
         23        INIT_METHOD_CALL                                         !1, 'getStartLine'
         24        DO_FCALL                                      0  $19     
         25        SUB                                              ~20     $19, 1
         26        SEND_VAL_EX                                              ~20
         27        DO_FCALL                                      0          
   28    28        ASSIGN                                                   !3, ''
   29    29      > JMP                                                      ->35
   30    30    >   INIT_METHOD_CALL                                         !2, 'current'
         31        DO_FCALL                                      0  $23     
         32        ASSIGN_OP                                     8          !3, $23
   31    33        INIT_METHOD_CALL                                         !2, 'next'
         34        DO_FCALL                                      0          
   29    35    >   INIT_METHOD_CALL                                         !2, 'key'
         36        DO_FCALL                                      0  $26     
         37        INIT_METHOD_CALL                                         !1, 'getEndLine'
         38        DO_FCALL                                      0  $27     
         39        IS_SMALLER                                               $26, $27
         40      > JMPNZ                                                    ~28, ->30
   33    41    >   INIT_FCALL                                               'md5'
         42        INIT_FCALL                                               'json_encode'
   34    43        INIT_ARRAY                                       ~31     !3
   35    44        INIT_METHOD_CALL                                         !1, 'getStaticVariables'
         45        DO_FCALL                                      0  $32     
         46        ADD_ARRAY_ELEMENT                                ~31     $32
         47        SEND_VAL                                                 ~31
         48        DO_ICALL                                         $33     
         49        SEND_VAR                                                 $33
         50        DO_ICALL                                         $34     
   33    51        FETCH_STATIC_PROP_W          unknown             $29     'hashes'
         52        ASSIGN_DIM                                               $29, !0
   35    53        OP_DATA                                                  $34
   38    54    >   FETCH_STATIC_PROP_R          unknown             ~35     'hashes'
         55        FETCH_DIM_R                                      ~36     ~35, !0
         56      > RETURN                                                   ~36
   39    57*     > RETURN                                                   null

End of function from

End of class ClosureHash.

Class Test:
Function hello:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IDT6m
function name:  hello
number of ops:  7
compiled vars:  !0 = $greeting, !1 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FIDT6m%3A46%240'
          2        BIND_LEXICAL                                             ~2, !0
          3        BIND_LEXICAL                                             ~2, !1
          4        ASSIGN                                                   !1, ~2
   49     5      > RETURN                                                   !1
   50     6*     > RETURN                                                   null

End of function hello

End of class Test.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.98 ms | 1404 KiB | 17 Q