3v4l.org

run code in 300+ PHP versions simultaneously
<?php $records = [ 't' => new WeakMap(), ]; class X { public array $id; public function __destruct() { var_dump('Cleaning up: ' . json_encode($this->id)); } } // Treat this as usages external to Records itself. $external = []; (function () use (&$records, &$external) { foreach (range(0, 3) as $i) { $x = new X(); $x->id = [$i]; $records['t'][$x] = WeakReference::create($x); $external[] = $x; } })(); $finder = function (array $find) use (&$records) { foreach ($records['t'] as $object => $ref) { if ($object->id == $find) { return $object; } } }; var_dump('Found object:', $finder([2])); var_dump('Found object:', $finder([99])); var_dump(count($records['t'])); var_dump('Removing external 2 (should auto-destruct)'); unset($external[2]); var_dump(count($records['t'])); var_dump('Exiting...');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yug4b
function name:  (null)
number of ops:  45
compiled vars:  !0 = $records, !1 = $external, !2 = $finder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   NEW                                              $3      'WeakMap'
          1        DO_FCALL                                      0          
          2        INIT_ARRAY                                       ~5      $3, 't'
    3     3        ASSIGN                                                   !0, ~5
   14     4        ASSIGN                                                   !1, <array>
   16     5        DECLARE_LAMBDA_FUNCTION                          ~8      [0]
          6        BIND_LEXICAL                                             ~8, !0
          7        BIND_LEXICAL                                             ~8, !1
   24     8        INIT_DYNAMIC_CALL                                        ~8
          9        DO_FCALL                                      0          
   26    10        DECLARE_LAMBDA_FUNCTION                          ~10     [1]
         11        BIND_LEXICAL                                             ~10, !0
         12        ASSIGN                                                   !2, ~10
   34    13        INIT_FCALL                                               'var_dump'
         14        SEND_VAL                                                 'Found+object%3A'
         15        INIT_DYNAMIC_CALL                                        !2
         16        SEND_VAL_EX                                              <array>
         17        DO_FCALL                                      0  $12     
         18        SEND_VAR                                                 $12
         19        DO_ICALL                                                 
   35    20        INIT_FCALL                                               'var_dump'
         21        SEND_VAL                                                 'Found+object%3A'
         22        INIT_DYNAMIC_CALL                                        !2
         23        SEND_VAL_EX                                              <array>
         24        DO_FCALL                                      0  $14     
         25        SEND_VAR                                                 $14
         26        DO_ICALL                                                 
   37    27        INIT_FCALL                                               'var_dump'
         28        FETCH_DIM_R                                      ~16     !0, 't'
         29        COUNT                                            ~17     ~16
         30        SEND_VAL                                                 ~17
         31        DO_ICALL                                                 
   38    32        INIT_FCALL                                               'var_dump'
         33        SEND_VAL                                                 'Removing+external+2+%28should+auto-destruct%29'
         34        DO_ICALL                                                 
   39    35        UNSET_DIM                                                !1, 2
   41    36        INIT_FCALL                                               'var_dump'
         37        FETCH_DIM_R                                      ~20     !0, 't'
         38        COUNT                                            ~21     ~20
         39        SEND_VAL                                                 ~21
         40        DO_ICALL                                                 
   43    41        INIT_FCALL                                               'var_dump'
         42        SEND_VAL                                                 'Exiting...'
         43        DO_ICALL                                                 
         44      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 23
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/Yug4b
function name:  {closure}
number of ops:  25
compiled vars:  !0 = $records, !1 = $external, !2 = $i, !3 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   17     2        INIT_FCALL                                               'range'
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 3
          5        DO_ICALL                                         $4      
          6      > FE_RESET_R                                       $5      $4, ->23
          7    > > FE_FETCH_R                                               $5, !2, ->23
   18     8    >   NEW                                              $6      'X'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $6
   19    11        INIT_ARRAY                                       ~10     !2
         12        ASSIGN_OBJ                                               !3, 'id'
         13        OP_DATA                                                  ~10
   20    14        INIT_STATIC_METHOD_CALL                                  'WeakReference', 'create'
         15        SEND_VAR                                                 !3
         16        DO_FCALL                                      0  $13     
         17        FETCH_DIM_W                                      $11     !0, 't'
         18        ASSIGN_DIM                                               $11, !3
         19        OP_DATA                                                  $13
   22    20        ASSIGN_DIM                                               !1
         21        OP_DATA                                                  !3
   17    22      > JMP                                                      ->7
         23    >   FE_FREE                                                  $5
   24    24      > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/Yug4b
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $find, !1 = $records, !2 = $ref, !3 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   27     2        FETCH_DIM_R                                      ~4      !1, 't'
          3      > FE_RESET_R                                       $5      ~4, ->12
          4    > > FE_FETCH_R                                       ~6      $5, !2, ->12
          5    >   ASSIGN                                                   !3, ~6
   28     6        FETCH_OBJ_R                                      ~8      !3, 'id'
          7        IS_EQUAL                                                 !0, ~8
          8      > JMPZ                                                     ~9, ->11
   29     9    >   FE_FREE                                                  $5
         10      > RETURN                                                   !3
   27    11    > > JMP                                                      ->4
         12    >   FE_FREE                                                  $5
   32    13      > RETURN                                                   null

End of Dynamic Function 1

Class X:
Function __destruct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yug4b
function name:  __destruct
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'json_encode'
          2        FETCH_OBJ_R                                      ~0      'id'
          3        SEND_VAL                                                 ~0
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      'Cleaning+up%3A+', $1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                                 
          8      > RETURN                                                   null

End of function __destruct

End of class X.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
135.24 ms | 1009 KiB | 16 Q