3v4l.org

run code in 300+ PHP versions simultaneously
<?php // This is the class that we want to make lazy. // Note that this is an opaque class from the PoV of the proxy-generation logic: // its implementation details are unknown. class A { protected array $data = []; public function __construct() { $this->data = ['foo' => 123]; } public function __get($n) { return $this->data[$n] ?? null; } // line 15 public function __set($n, $v) { $this->data[$n] = $v; } public function __isset($n) { return isset($this->data[$n]); } public function __unset($n) { unset($this->data[$n]); } } // This is a simplified proxy class that would typically be generated automatically // from the signature of the proxied class. The way this works is by unsetting all // declared properties at instantiation time, then using the magic methods to lazily // initialize them on demand. This is a very simple implementation that does not handle // all edge cases, but it should be enough to illustrate the issue. The logic in each // magic method is very repetitive: we checks if we're trying to access the uninitialized // property. If yes, we initialize it, if not, we call the parent method. class LazyA extends A { private const STATUS_UNINITIALIZED = 0; private const STATUS_INITIALIZING = 1; private const STATUS_INITIALIZED = 2; private $lazyStatus = self::STATUS_UNINITIALIZED; public function __construct() { unset($this->data); } public function __get($n) { if (self::STATUS_INITIALIZED === $this->lazyStatus || 'data' !== $n) { if ('data' === $n && (new ReflectionProperty($this, 'data'))->isInitialized($this)) { return $this->data; } return parent::__get($n); } $this->initialize(); return $this->data; } public function __set($n, $v) { if (self::STATUS_INITIALIZED === $this->lazyStatus || 'data' !== $n) { parent::__set($n, $v); } $this->initialize(); $this->data = $v; } public function __isset($n) { if (self::STATUS_INITIALIZED === $this->lazyStatus || 'data' !== $n) { return parent::__isset($n); } $this->initialize(); return isset($this->data); } public function __unset($n) { if (self::STATUS_INITIALIZED === $this->lazyStatus || 'data' !== $n) { parent::__unset($n); } $this->initialize(); unset($this->data); } private function initialize() { if (self::STATUS_INITIALIZING === $this->lazyStatus) { return; } $this->lazyStatus = self::STATUS_INITIALIZING; parent::__construct(); $this->lazyStatus = self::STATUS_INITIALIZED; } } $a = new LazyA(); // Currently, this will trigger a TypeError: // Cannot assign null to property A::$data of type array on line 15 // With Ilija's patch, this will work as expected and will display int(123) var_dump($a->foo);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  (null)
number of ops:  8
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   NEW                                              $1      'LazyA'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
  105     3        INIT_FCALL                                               'var_dump'
          4        FETCH_OBJ_R                                      ~4      !0, 'foo'
          5        SEND_VAL                                                 ~4
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Class A:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  __construct
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   ASSIGN_OBJ                                               'data'
          1        OP_DATA                                                  <array>
   13     2      > 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/fKnlq
function name:  __get
number of ops:  7
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        FETCH_OBJ_IS                                     ~1      'data'
          2        FETCH_DIM_IS                                     ~2      ~1, !0
          3        COALESCE                                         ~3      ~2
          4        QM_ASSIGN                                        ~3      null
          5      > RETURN                                                   ~3
          6*     > RETURN                                                   null

End of function __get

Function __set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  __set
number of ops:  6
compiled vars:  !0 = $n, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        FETCH_OBJ_W                                      $2      'data'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
          5      > RETURN                                                   null

End of function __set

Function __isset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  __isset
number of ops:  5
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        FETCH_OBJ_IS                                     ~1      'data'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function __isset

Function __unset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  __unset
number of ops:  4
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        FETCH_OBJ_UNSET                                  $1      'data'
          2        UNSET_DIM                                                $1, !0
          3      > RETURN                                                   null

End of function __unset

End of class A.

Class LazyA:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  __construct
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   UNSET_OBJ                                                'data'
   39     1      > RETURN                                                   null

End of function __construct

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 26
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/fKnlq
function name:  __get
number of ops:  31
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        FETCH_OBJ_R                                      ~1      'lazyStatus'
          2        IS_IDENTICAL                                     ~2      ~1, 2
          3      > JMPNZ_EX                                         ~2      ~2, ->6
          4    >   IS_NOT_IDENTICAL                                 ~3      !0, 'data'
          5        BOOL                                             ~2      ~3
          6    > > JMPZ                                                     ~2, ->26
   44     7    >   IS_IDENTICAL                                     ~4      !0, 'data'
          8      > JMPZ_EX                                          ~4      ~4, ->19
          9    >   NEW                                              $5      'ReflectionProperty'
         10        FETCH_THIS                                       $6      
         11        SEND_VAR_EX                                              $6
         12        SEND_VAL_EX                                              'data'
         13        DO_FCALL                                      0          
         14        INIT_METHOD_CALL                                         $5, 'isInitialized'
         15        FETCH_THIS                                       $8      
         16        SEND_VAR_EX                                              $8
         17        DO_FCALL                                      0  $9      
         18        BOOL                                             ~4      $9
         19    > > JMPZ                                                     ~4, ->22
   45    20    >   FETCH_OBJ_R                                      ~10     'data'
         21      > RETURN                                                   ~10
   47    22    >   INIT_STATIC_METHOD_CALL                                  '__get'
         23        SEND_VAR_EX                                              !0
         24        DO_FCALL                                      0  $11     
         25      > RETURN                                                   $11
   50    26    >   INIT_METHOD_CALL                                         'initialize'
         27        DO_FCALL                                      0          
   52    28        FETCH_OBJ_R                                      ~13     'data'
         29      > RETURN                                                   ~13
   53    30*     > RETURN                                                   null

End of function __get

Function __set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
Branch analysis from position: 7
filename:       /in/fKnlq
function name:  __set
number of ops:  17
compiled vars:  !0 = $n, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   57     2        FETCH_OBJ_R                                      ~2      'lazyStatus'
          3        IS_IDENTICAL                                     ~3      ~2, 2
          4      > JMPNZ_EX                                         ~3      ~3, ->7
          5    >   IS_NOT_IDENTICAL                                 ~4      !0, 'data'
          6        BOOL                                             ~3      ~4
          7    > > JMPZ                                                     ~3, ->12
   58     8    >   INIT_STATIC_METHOD_CALL                                  '__set'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
   61    12    >   INIT_METHOD_CALL                                         'initialize'
         13        DO_FCALL                                      0          
   63    14        ASSIGN_OBJ                                               'data'
         15        OP_DATA                                                  !1
   64    16      > RETURN                                                   null

End of function __set

Function __isset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/fKnlq
function name:  __isset
number of ops:  16
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
   68     1        FETCH_OBJ_R                                      ~1      'lazyStatus'
          2        IS_IDENTICAL                                     ~2      ~1, 2
          3      > JMPNZ_EX                                         ~2      ~2, ->6
          4    >   IS_NOT_IDENTICAL                                 ~3      !0, 'data'
          5        BOOL                                             ~2      ~3
          6    > > JMPZ                                                     ~2, ->11
   69     7    >   INIT_STATIC_METHOD_CALL                                  '__isset'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0  $4      
         10      > RETURN                                                   $4
   72    11    >   INIT_METHOD_CALL                                         'initialize'
         12        DO_FCALL                                      0          
   74    13        ISSET_ISEMPTY_PROP_OBJ                           ~6      'data'
         14      > RETURN                                                   ~6
   75    15*     > RETURN                                                   null

End of function __isset

Function __unset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
Branch analysis from position: 6
filename:       /in/fKnlq
function name:  __unset
number of ops:  14
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV                                             !0      
   79     1        FETCH_OBJ_R                                      ~1      'lazyStatus'
          2        IS_IDENTICAL                                     ~2      ~1, 2
          3      > JMPNZ_EX                                         ~2      ~2, ->6
          4    >   IS_NOT_IDENTICAL                                 ~3      !0, 'data'
          5        BOOL                                             ~2      ~3
          6    > > JMPZ                                                     ~2, ->10
   80     7    >   INIT_STATIC_METHOD_CALL                                  '__unset'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
   83    10    >   INIT_METHOD_CALL                                         'initialize'
         11        DO_FCALL                                      0          
   85    12        UNSET_OBJ                                                'data'
   86    13      > RETURN                                                   null

End of function __unset

Function initialize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fKnlq
function name:  initialize
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   FETCH_OBJ_R                                      ~0      'lazyStatus'
          1        IS_IDENTICAL                                             ~0, 1
          2      > JMPZ                                                     ~1, ->4
   91     3    > > RETURN                                                   null
   94     4    >   ASSIGN_OBJ                                               'lazyStatus'
          5        OP_DATA                                                  1
   95     6        INIT_STATIC_METHOD_CALL                                  
          7        DO_FCALL                                      0          
   96     8        ASSIGN_OBJ                                               'lazyStatus'
          9        OP_DATA                                                  2
   97    10      > RETURN                                                   null

End of function initialize

End of class LazyA.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.01 ms | 1453 KiB | 14 Q