3v4l.org

run code in 500+ PHP versions simultaneously
<?php class a { public readonly b $b; function __construct(public string $name) { $this->b = new b($this); } function hoohoo(): mixed { return $this->b; } } class b { use weakObjReference; function __construct(a $container) { $this->weakReference($container, 'container'); // $this->container "created" } function heehee($name): mixed { $x = $name . ', '. $this->container->name; return $x; } } $a = new a('world 1'); var_dump($a->hoohoo()->heehee('hello')); // EXAMPLE 1 var_dump((new a('world 2'))->hoohoo()->heehee('good afternoon')); // EXAMPLE 2. trait weakObjReference { // Door containersnt als weak references op te slaan, voorkom je dat container references reference counts verhogen waardoor // cache niet geleegd wordt "want deze instance wordt nog gebruikt". // Ook nuttig bij debugging aangezien bij var_debug c.s. niet het hele object wordt weergegeven // Gebruik: kan gewone eigenschappen vervangen. // $obj->weakReference($containingObject, 'container'); // Vanaf nu bruikbaar: // $obj->container // NOTE: ook bruikbaar voor grandcontainers, sibling containers, etc. protected array $weakObjectStorage = []; protected function weakReference(object $container, string $propertyName='containerc'/*, bool $serializeable=true*/) { $this->weakObjectStorage[$propertyName] = new weakObjReferenceStorage($container); } // Have your own __get? Let it call __getWeakRef() to see if the container is needed function __get(string $propName) { if (! array_key_exists($propName, $this->weakObjectStorage)) { if ( method_exists($this, $propName)) trigger_error("Error in ".__CLASS__. ": unknown property $propName, did you mean the method ".__CLASS__."::$propName() ?", E_USER_ERROR); trigger_error("Error in ".__CLASS__. ": unknown property '$propName'", E_USER_ERROR); } return $this->__getWeakRef($propName); } // Alleen nodig voor in eigen __get() routines function __getWeakRef(string $propName): ?object { if (isset($this->weakObjectStorage[$propName])) { $r = $this->weakObjectStorage[$propName]->get() ; return $r; } return null ; } function __isset(string $propName): bool { return isset($this->$propName) || $this->__issetWeakRef($propName) ; } // Have your own __isset? Let it call __issetWeakRef() to see if it is set as a weak reference function __issetWeakRef(string $propName): bool { return isset($this->weakObjectStorage[$propName]); } private array $temporaryStrongReferences = []; // Temporarily strengthen the reference public function temporaryStrong($property) { if (isset($this->weakObjectStorage[$property])) { $this->temporaryStrongReferences[$property] = $this->weakObjectStorage[$property]->get(); } } // Revert to weak reference public function weakAgain($property) { unset($this->temporaryStrongReferences[$property]); } } class weakObjReferenceStorage { private readonly WeakReference $weakRef; function __construct(object $object) { if ( get_class($object) == 'WeakReference') // VDZ $this->WeakRef = $object; else $this->weakRef = WeakReference::create($object); } public function get(): object { return $this->weakRef->get(); } function __serialize() { // serialize the original object because weakreferences cannot be serialised return ['weakRef'=>$this->weakRef->get()]; } function __unserialize(array $array) { // restore as a weak reference $this->weakRef = WeakReference::create($array['weakRef']); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  (null)
number of ops:  25
compiled vars:  !0 = $a
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   DECLARE_CLASS                                                'b'
   33     1        NEW                                                  $1      'a'
          2        SEND_VAL_EX                                                  'world+1'
          3        DO_FCALL                                          0          
          4        ASSIGN                                                       !0, $1
   34     5        INIT_FCALL                                                   'var_dump'
          6        INIT_METHOD_CALL                                             !0, 'hoohoo'
          7        DO_FCALL                                          0  $4      
          8        INIT_METHOD_CALL                                             $4, 'heehee'
          9        SEND_VAL_EX                                                  'hello'
         10        DO_FCALL                                          0  $5      
         11        SEND_VAR                                                     $5
         12        DO_ICALL                                                     
   35    13        INIT_FCALL                                                   'var_dump'
         14        NEW                                                  $7      'a'
         15        SEND_VAL_EX                                                  'world+2'
         16        DO_FCALL                                          0          
         17        INIT_METHOD_CALL                                             $7, 'hoohoo'
         18        DO_FCALL                                          0  $9      
         19        INIT_METHOD_CALL                                             $9, 'heehee'
         20        SEND_VAL_EX                                                  'good+afternoon'
         21        DO_FCALL                                          0  $10     
         22        SEND_VAR                                                     $10
         23        DO_ICALL                                                     
  137    24      > RETURN                                                       1

Class a:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __construct
number of ops:  10
compiled vars:  !0 = $name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    8     0  E >   RECV                                                 !0      
          1        ASSIGN_OBJ                                                   'name'
          2        OP_DATA                                                      !0
    9     3        NEW                                                  $2      'b'
          4        FETCH_THIS                                           $3      
          5        SEND_VAR_EX                                                  $3
          6        DO_FCALL                                          0          
          7        ASSIGN_OBJ                                                   'b'
          8        OP_DATA                                                      $2
   10     9      > RETURN                                                       null

End of function __construct

Function hoohoo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  hoohoo
number of ops:  4
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   FETCH_OBJ_R                                          ~0      'b'
          1      > RETURN                                                       ~0
   14     2*       VERIFY_RETURN_TYPE                                           
          3*     > RETURN                                                       null

End of function hoohoo

End of class a.

Class b:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __construct
number of ops:  6
compiled vars:  !0 = $container
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
   23     1        INIT_METHOD_CALL                                             'weakReference'
          2        SEND_VAR_EX                                                  !0
          3        SEND_VAL_EX                                                  'container'
          4        DO_FCALL                                          0          
   24     5      > RETURN                                                       null

End of function __construct

Function heehee:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  heehee
number of ops:  9
compiled vars:  !0 = $name, !1 = $x
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   RECV                                                 !0      
   27     1        CONCAT                                               ~2      !0, '%2C+'
          2        FETCH_OBJ_R                                          ~3      'container'
          3        FETCH_OBJ_R                                          ~4      ~3, 'name'
          4        CONCAT                                               ~5      ~2, ~4
          5        ASSIGN                                                       !1, ~5
   28     6      > RETURN                                                       !1
   30     7*       VERIFY_RETURN_TYPE                                           
          8*     > RETURN                                                       null

End of function heehee

End of class b.

Class weakObjReference:
Function weakreference:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  weakReference
number of ops:  9
compiled vars:  !0 = $container, !1 = $propertyName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   60     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      'containerc'
   61     2        NEW                                                  $4      'weakObjReferenceStorage'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0          
          5        FETCH_OBJ_W                                          $2      'weakObjectStorage'
          6        ASSIGN_DIM                                                   $2, !1
          7        OP_DATA                                                      $4
   62     8      > RETURN                                                       null

End of function weakreference

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 37
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 27
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
Branch analysis from position: 37
filename:       /in/7DpN2
function name:  __get
number of ops:  42
compiled vars:  !0 = $propName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   65     0  E >   RECV                                                 !0      
   66     1        FETCH_OBJ_R                                          ~1      'weakObjectStorage'
          2        ARRAY_KEY_EXISTS                                     ~2      !0, ~1
          3        BOOL_NOT                                             ~3      ~2
          4      > JMPZ                                                         ~3, ->37
   67     5    >   INIT_FCALL                                                   'method_exists'
          6        FETCH_THIS                                           ~4      
          7        SEND_VAL                                                     ~4
          8        SEND_VAR                                                     !0
          9        DO_ICALL                                             $5      
         10      > JMPZ                                                         $5, ->27
   68    11    >   INIT_FCALL                                                   'trigger_error'
         12        FETCH_CLASS_NAME                                     ~6      
         13        CONCAT                                               ~7      'Error+in+', ~6
         14        ROPE_INIT                                         3  ~9      '%3A+unknown+property+'
         15        ROPE_ADD                                          1  ~9      ~9, !0
         16        ROPE_END                                          2  ~8      ~9, '%2C+did+you+mean+the+method+'
         17        CONCAT                                               ~11     ~7, ~8
         18        FETCH_CLASS_NAME                                     ~12     
         19        CONCAT                                               ~13     ~11, ~12
         20        ROPE_INIT                                         3  ~15     '%3A%3A'
         21        ROPE_ADD                                          1  ~15     ~15, !0
         22        ROPE_END                                          2  ~14     ~15, '%28%29+%3F'
         23        CONCAT                                               ~17     ~13, ~14
         24        SEND_VAL                                                     ~17
         25        SEND_VAL                                                     256
         26        DO_ICALL                                                     
   69    27    >   INIT_FCALL                                                   'trigger_error'
         28        FETCH_CLASS_NAME                                     ~19     
         29        CONCAT                                               ~20     'Error+in+', ~19
         30        ROPE_INIT                                         3  ~22     '%3A+unknown+property+%27'
         31        ROPE_ADD                                          1  ~22     ~22, !0
         32        ROPE_END                                          2  ~21     ~22, '%27'
         33        CONCAT                                               ~24     ~20, ~21
         34        SEND_VAL                                                     ~24
         35        SEND_VAL                                                     256
         36        DO_ICALL                                                     
   71    37    >   INIT_METHOD_CALL                                             '__getWeakRef'
         38        SEND_VAR_EX                                                  !0
         39        DO_FCALL                                          0  $26     
         40      > RETURN                                                       $26
   72    41*     > RETURN                                                       null

End of function __get

Function __getweakref:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __getWeakRef
number of ops:  14
compiled vars:  !0 = $propName, !1 = $r
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   75     0  E >   RECV                                                 !0      
   76     1        FETCH_OBJ_IS                                         ~2      'weakObjectStorage'
          2        ISSET_ISEMPTY_DIM_OBJ                             0          ~2, !0
          3      > JMPZ                                                         ~3, ->11
   77     4    >   FETCH_OBJ_R                                          ~4      'weakObjectStorage'
          5        FETCH_DIM_R                                          ~5      ~4, !0
          6        INIT_METHOD_CALL                                             ~5, 'get'
          7        DO_FCALL                                          0  $6      
          8        ASSIGN                                                       !1, $6
   78     9        VERIFY_RETURN_TYPE                                           !1
         10      > RETURN                                                       !1
   80    11    > > RETURN                                                       null
   81    12*       VERIFY_RETURN_TYPE                                           
         13*     > RETURN                                                       null

End of function __getweakref

Function __isset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/7DpN2
function name:  __isset
number of ops:  11
compiled vars:  !0 = $propName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   83     0  E >   RECV                                                 !0      
   84     1        ISSET_ISEMPTY_PROP_OBJ                               ~1      !0
          2      > JMPNZ_EX                                             ~1      ~1, ->7
          3    >   INIT_METHOD_CALL                                             '__issetWeakRef'
          4        SEND_VAR_EX                                                  !0
          5        DO_FCALL                                          0  $2      
          6        BOOL                                                 ~1      $2
          7    >   VERIFY_RETURN_TYPE                                           ~1
          8      > RETURN                                                       ~1
   85     9*       VERIFY_RETURN_TYPE                                           
         10*     > RETURN                                                       null

End of function __isset

Function __issetweakref:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __issetWeakRef
number of ops:  7
compiled vars:  !0 = $propName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   88     0  E >   RECV                                                 !0      
   89     1        FETCH_OBJ_IS                                         ~1      'weakObjectStorage'
          2        ISSET_ISEMPTY_DIM_OBJ                             0  ~2      ~1, !0
          3        VERIFY_RETURN_TYPE                                           ~2
          4      > RETURN                                                       ~2
   90     5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function __issetweakref

Function temporarystrong:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/7DpN2
function name:  temporaryStrong
number of ops:  12
compiled vars:  !0 = $property
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   96     0  E >   RECV                                                 !0      
   97     1        FETCH_OBJ_IS                                         ~1      'weakObjectStorage'
          2        ISSET_ISEMPTY_DIM_OBJ                             0          ~1, !0
          3      > JMPZ                                                         ~2, ->11
   98     4    >   FETCH_OBJ_R                                          ~5      'weakObjectStorage'
          5        FETCH_DIM_R                                          ~6      ~5, !0
          6        INIT_METHOD_CALL                                             ~6, 'get'
          7        DO_FCALL                                          0  $7      
          8        FETCH_OBJ_W                                          $3      'temporaryStrongReferences'
          9        ASSIGN_DIM                                                   $3, !0
         10        OP_DATA                                                      $7
  100    11    > > RETURN                                                       null

End of function temporarystrong

Function weakagain:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  weakAgain
number of ops:  4
compiled vars:  !0 = $property
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  103     0  E >   RECV                                                 !0      
  104     1        FETCH_OBJ_UNSET                                      $1      'temporaryStrongReferences'
          2        UNSET_DIM                                                    $1, !0
  105     3      > RETURN                                                       null

End of function weakagain

End of class weakObjReference.

Class weakObjReferenceStorage:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __construct
number of ops:  13
compiled vars:  !0 = $object
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  116     0  E >   RECV                                                 !0      
  117     1        GET_CLASS                                            ~1      !0
          2        IS_EQUAL                                                     ~1, 'WeakReference'
          3      > JMPZ                                                         ~2, ->7
  118     4    >   ASSIGN_OBJ                                                   'WeakRef'
          5        OP_DATA                                                      !0
  117     6      > JMP                                                          ->12
  120     7    >   INIT_STATIC_METHOD_CALL                                      'WeakReference', 'create'
          8        SEND_VAR                                                     !0
          9        DO_FCALL                                          0  $5      
         10        ASSIGN_OBJ                                                   'weakRef'
         11        OP_DATA                                                      $5
  121    12    > > RETURN                                                       null

End of function __construct

Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  get
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  124     0  E >   FETCH_OBJ_R                                          ~0      'weakRef'
          1        INIT_METHOD_CALL                                             ~0, 'get'
          2        DO_FCALL                                          0  $1      
          3        VERIFY_RETURN_TYPE                                           $1
          4      > RETURN                                                       $1
  125     5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function get

Function __serialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __serialize
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  129     0  E >   FETCH_OBJ_R                                          ~0      'weakRef'
          1        INIT_METHOD_CALL                                             ~0, 'get'
          2        DO_FCALL                                          0  $1      
          3        INIT_ARRAY                                           ~2      $1, 'weakRef'
          4      > RETURN                                                       ~2
  131     5*     > RETURN                                                       null

End of function __serialize

Function __unserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7DpN2
function name:  __unserialize
number of ops:  8
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  132     0  E >   RECV                                                 !0      
  134     1        INIT_STATIC_METHOD_CALL                                      'WeakReference', 'create'
          2        FETCH_DIM_R                                          ~2      !0, 'weakRef'
          3        SEND_VAL                                                     ~2
          4        DO_FCALL                                          0  $3      
          5        ASSIGN_OBJ                                                   'weakRef'
          6        OP_DATA                                                      $3
  135     7      > RETURN                                                       null

End of function __unserialize

End of class weakObjReferenceStorage.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
165.3 ms | 1655 KiB | 16 Q