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() { var_dump('lazy loading!'); $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() { var_dump('lazy loading!'); $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('Trying classic lazy-loading via method overrides:'); $classic = new ClassicLazyFoo(); var_dump($classic->getBar()); var_dump($classic->getBar()); var_dump($classic->getBar()); var_dump('Trying lazy-loading via undefined properties:'); $lazyProperty = new LazyPropertyLazyFoo(); var_dump($lazyProperty->getBar()); var_dump($lazyProperty->getBar()); var_dump($lazyProperty->getBar());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vJ36M
function name:  (null)
number of ops:  43
compiled vars:  !0 = $classic, !1 = $lazyProperty
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Trying+classic+lazy-loading+via+method+overrides%3A'
          2        DO_ICALL                                                 
   64     3        NEW                                              $3      'ClassicLazyFoo'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $3
   66     6        INIT_FCALL                                               'var_dump'
          7        INIT_METHOD_CALL                                         !0, 'getBar'
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_ICALL                                                 
   67    11        INIT_FCALL                                               'var_dump'
         12        INIT_METHOD_CALL                                         !0, 'getBar'
         13        DO_FCALL                                      0  $8      
         14        SEND_VAR                                                 $8
         15        DO_ICALL                                                 
   68    16        INIT_FCALL                                               'var_dump'
         17        INIT_METHOD_CALL                                         !0, 'getBar'
         18        DO_FCALL                                      0  $10     
         19        SEND_VAR                                                 $10
         20        DO_ICALL                                                 
   70    21        INIT_FCALL                                               'var_dump'
         22        SEND_VAL                                                 'Trying+lazy-loading+via+undefined+properties%3A'
         23        DO_ICALL                                                 
   72    24        NEW                                              $13     'LazyPropertyLazyFoo'
         25        DO_FCALL                                      0          
         26        ASSIGN                                                   !1, $13
   74    27        INIT_FCALL                                               'var_dump'
         28        INIT_METHOD_CALL                                         !1, 'getBar'
         29        DO_FCALL                                      0  $16     
         30        SEND_VAR                                                 $16
         31        DO_ICALL                                                 
   75    32        INIT_FCALL                                               'var_dump'
         33        INIT_METHOD_CALL                                         !1, 'getBar'
         34        DO_FCALL                                      0  $18     
         35        SEND_VAR                                                 $18
         36        DO_ICALL                                                 
   76    37        INIT_FCALL                                               'var_dump'
         38        INIT_METHOD_CALL                                         !1, 'getBar'
         39        DO_FCALL                                      0  $20     
         40        SEND_VAR                                                 $20
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

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

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

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

End of function %00%7Bclosure%7D%2Fin%2FvJ36M%3A42%241

Function %00%7Bclosure%7D%2Fin%2FvJ36M%3A50%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vJ36M
function name:  {closure}
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'lazy+loading%21'
          2        DO_ICALL                                                 
   53     3        FETCH_THIS                                       $1      
          4        ASSIGN_OBJ                                               $1, 'bar'
          5        OP_DATA                                                  'Loaded+from+DB%21'
   54     6      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FvJ36M%3A50%242

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

End of function %00%7Bclosure%7D%2Fin%2FvJ36M%3A56%243

Class Foo:
Function getbar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vJ36M
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/vJ36M
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/vJ36M
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%2FvJ36M%3A29%240'
   33     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          
   34    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/vJ36M
function name:  __construct
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FvJ36M%3A42%241'
   44     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          
   45    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/vJ36M
function name:  __get
number of ops:  30
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   50     1        INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FvJ36M%3A50%242'
   54     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          
   56    14        INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
         15        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FvJ36M%3A56%243'
         16        BIND_LEXICAL                                             ~7, !0
   58    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
   59    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/vJ36M
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:
153.77 ms | 1412 KiB | 17 Q