3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $bar = 'baz'; public function getBar() { return $this->bar; } } // http://3v4l.org/pc6lo#v540 class ClassicLazyFoo extends Foo { private $initialized = false; public function getBar() { $this->init(); return parent::getBar(); } private function init() { if ($this->initialized) { return; } \Closure::bind(function() { $this->bar = 'Loaded from DB!'; }, $this, get_parent_class($this))->__invoke(); } } // look ma: no method overrides! class LazyPropertyLazyFoo extends Foo { public function __construct() { \Closure::bind(function() { unset($this->bar); }, $this, get_parent_class($this))->__invoke(); } // note that other accessors also need to be created for full functional completeness public function __get($name) { \Closure::bind(function() { $this->bar = 'Loaded from DB!'; }, $this, get_parent_class($this))->__invoke(); return \Closure::bind(function() use ($name) { return $this->$name; }, $this, get_parent_class($this))->__invoke(); } } var_dump((new ClassicLazyFoo())->getBar()); var_dump((new LazyPropertyLazyFoo())->getBar());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  (null)
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   INIT_FCALL                                               'var_dump'
          1        NEW                                              $0      'ClassicLazyFoo'
          2        DO_FCALL                                      0          
          3        INIT_METHOD_CALL                                         $0, 'getBar'
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
   60     7        INIT_FCALL                                               'var_dump'
          8        NEW                                              $4      'LazyPropertyLazyFoo'
          9        DO_FCALL                                      0          
         10        INIT_METHOD_CALL                                         $4, 'getBar'
         11        DO_FCALL                                      0  $6      
         12        SEND_VAR                                                 $6
         13        DO_ICALL                                                 
         14      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F1sBCg%3A29%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   FETCH_THIS                                       $0      
          1        ASSIGN_OBJ                                               $0, 'bar'
          2        OP_DATA                                                  'Loaded+from+DB%21'
   31     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1sBCg%3A29%240

Function %00%7Bclosure%7D%2Fin%2F1sBCg%3A40%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  {closure}
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   FETCH_THIS                                       $0      
          1        UNSET_OBJ                                                $0, 'bar'
   42     2      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1sBCg%3A40%241

Function %00%7Bclosure%7D%2Fin%2F1sBCg%3A48%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   FETCH_THIS                                       $0      
          1        ASSIGN_OBJ                                               $0, 'bar'
          2        OP_DATA                                                  'Loaded+from+DB%21'
   50     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1sBCg%3A48%242

Function %00%7Bclosure%7D%2Fin%2F1sBCg%3A52%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   BIND_STATIC                                              !0
   53     1        FETCH_THIS                                       $1      
          2        FETCH_OBJ_R                                      ~2      $1, !0
          3      > RETURN                                                   ~2
   54     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1sBCg%3A52%243

Class Foo:
Function getbar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  getBar
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   FETCH_OBJ_R                                      ~0      'bar'
          1      > RETURN                                                   ~0
   10     2*     > RETURN                                                   null

End of function getbar

End of class Foo.

Class ClassicLazyFoo:
Function getbar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  getBar
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   INIT_METHOD_CALL                                         'init'
          1        DO_FCALL                                      0          
   22     2        INIT_STATIC_METHOD_CALL                                  'getBar'
          3        DO_FCALL                                      0  $1      
          4      > RETURN                                                   $1
   23     5*     > RETURN                                                   null

End of function getbar

Function init:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 3
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  init
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   FETCH_OBJ_R                                      ~0      'initialized'
          1      > JMPZ                                                     ~0, ->3
          2    > > RETURN                                                   null
   29     3    >   INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1sBCg%3A29%240'
   31     5        SEND_VAL                                                 ~1
          6        FETCH_THIS                                       ~2      
          7        SEND_VAL                                                 ~2
          8        INIT_FCALL                                               'get_parent_class'
          9        FETCH_THIS                                       ~3      
         10        SEND_VAL                                                 ~3
         11        DO_ICALL                                         $4      
         12        SEND_VAR                                                 $4
         13        DO_FCALL                                      0  $5      
         14        INIT_METHOD_CALL                                         $5, '__invoke'
         15        DO_FCALL                                      0          
   32    16      > RETURN                                                   null

End of function init

End of class ClassicLazyFoo.

Class LazyPropertyLazyFoo:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  __construct
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1sBCg%3A40%241'
   42     2        SEND_VAL                                                 ~0
          3        FETCH_THIS                                       ~1      
          4        SEND_VAL                                                 ~1
          5        INIT_FCALL                                               'get_parent_class'
          6        FETCH_THIS                                       ~2      
          7        SEND_VAL                                                 ~2
          8        DO_ICALL                                         $3      
          9        SEND_VAR                                                 $3
         10        DO_FCALL                                      0  $4      
         11        INIT_METHOD_CALL                                         $4, '__invoke'
         12        DO_FCALL                                      0          
   43    13      > 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/1sBCg
function name:  __get
number of ops:  30
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   48     1        INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1sBCg%3A48%242'
   50     3        SEND_VAL                                                 ~1
          4        FETCH_THIS                                       ~2      
          5        SEND_VAL                                                 ~2
          6        INIT_FCALL                                               'get_parent_class'
          7        FETCH_THIS                                       ~3      
          8        SEND_VAL                                                 ~3
          9        DO_ICALL                                         $4      
         10        SEND_VAR                                                 $4
         11        DO_FCALL                                      0  $5      
         12        INIT_METHOD_CALL                                         $5, '__invoke'
         13        DO_FCALL                                      0          
   52    14        INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
         15        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1sBCg%3A52%243'
         16        BIND_LEXICAL                                             ~7, !0
   54    17        SEND_VAL                                                 ~7
         18        FETCH_THIS                                       ~8      
         19        SEND_VAL                                                 ~8
         20        INIT_FCALL                                               'get_parent_class'
         21        FETCH_THIS                                       ~9      
         22        SEND_VAL                                                 ~9
         23        DO_ICALL                                         $10     
         24        SEND_VAR                                                 $10
         25        DO_FCALL                                      0  $11     
         26        INIT_METHOD_CALL                                         $11, '__invoke'
         27        DO_FCALL                                      0  $12     
         28      > RETURN                                                   $12
   55    29*     > RETURN                                                   null

End of function __get

Function getbar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1sBCg
function name:  getBar
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   FETCH_OBJ_R                                      ~0      'bar'
          1      > RETURN                                                   ~0
   10     2*     > RETURN                                                   null

End of function getbar

End of class LazyPropertyLazyFoo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.09 ms | 1408 KiB | 17 Q