3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Its destructor brings the closure back to life by storing it in a global. class Resurrector { public $closure; public $self; public function __destruct() { $GLOBALS['resurrected'] = $this->closure; } } class Holder { public $castObject; public $resurrector; public $self; } $array = ['victim' => new stdClass(), 'key' => 'value']; $holder = new Holder(); $resurrector = new Resurrector(); // With only string keys, the cast object shares $array's hashtable. $holder->castObject = (object) $array; // The closure holds the same hashtable through its static vars. $resurrector->closure = function () use ($array) { return $array; }; // Self-references make both objects collectable only by the cycle collector. $holder->resurrector = $resurrector; $holder->self = $holder; $resurrector->self = $resurrector; unset($array); gc_collect_cycles(); unset($holder, $resurrector); gc_collect_cycles(); // Frees 'victim', though the resurrected closure still holds it. gc_collect_cycles(); // Reads the freed object. var_dump($resurrected()['victim']);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LkhFS
function name:  (null)
number of ops:  40
compiled vars:  !0 = $array, !1 = $holder, !2 = $resurrector, !3 = $resurrected
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   NEW                                                  $4      'stdClass'
          1        DO_FCALL                                          0          
          2        INIT_ARRAY                                           ~6      $4, 'victim'
          3        ADD_ARRAY_ELEMENT                                    ~6      'value', 'key'
          4        ASSIGN                                                       !0, ~6
   24     5        NEW                                                  $8      'Holder'
          6        DO_FCALL                                          0          
          7        ASSIGN                                                       !1, $8
   25     8        NEW                                                  $11     'Resurrector'
          9        DO_FCALL                                          0          
         10        ASSIGN                                                       !2, $11
   28    11        CAST                                              8  ~15     !0
         12        ASSIGN_OBJ                                                   !1, 'castObject'
         13        OP_DATA                                                      ~15
   31    14        DECLARE_LAMBDA_FUNCTION                              ~17     [0]
         15        BIND_LEXICAL                                                 ~17, !0
         16        ASSIGN_OBJ                                                   !2, 'closure'
   33    17        OP_DATA                                                      ~17
   36    18        ASSIGN_OBJ                                                   !1, 'resurrector'
         19        OP_DATA                                                      !2
   37    20        ASSIGN_OBJ                                                   !1, 'self'
         21        OP_DATA                                                      !1
   38    22        ASSIGN_OBJ                                                   !2, 'self'
         23        OP_DATA                                                      !2
   40    24        UNSET_CV                                                     !0
   41    25        INIT_FCALL                                                   'gc_collect_cycles'
         26        DO_ICALL                                                     
   43    27        UNSET_CV                                                     !1
         28        UNSET_CV                                                     !2
   44    29        INIT_FCALL                                                   'gc_collect_cycles'
         30        DO_ICALL                                                     
   45    31        INIT_FCALL                                                   'gc_collect_cycles'
         32        DO_ICALL                                                     
   47    33        INIT_FCALL                                                   'var_dump'
         34        INIT_DYNAMIC_CALL                                            !3
         35        DO_FCALL                                          0  $24     
         36        FETCH_DIM_R                                          ~25     $24, 'victim'
         37        SEND_VAL                                                     ~25
         38        DO_ICALL                                                     
         39      > 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/LkhFS
function name:  {closure:/in/LkhFS:31}
number of ops:  3
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     0  E >   BIND_STATIC                                                  !0
   32     1      > RETURN                                                       !0
   33     2*     > RETURN                                                       null

End of Dynamic Function 0

Class Resurrector:
Function __destruct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LkhFS
function name:  __destruct
number of ops:  4
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   FETCH_OBJ_R                                          ~1      'closure'
          1        FETCH_W                          global              $0      'resurrected'
          2        ASSIGN                                                       $0, ~1
   12     3      > RETURN                                                       null

End of function __destruct

End of class Resurrector.

Class Holder: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
127.24 ms | 484 KiB | 9 Q