3v4l.org

run code in 300+ PHP versions simultaneously
$value = null; $executed = false; return function () use ($producer, & $value, & $executed) { if (!$executed) { $value = $producer(); $executed = true; } return $value; }; <?php class Connection {} class Context { public function get_instance(){ static $i; $i ?: $i = new Connection(); return $i; } } class A extends Context {} class B extends A {} class Context2 { public function __constructor() { $this->conn = new Connection(); // Should be lazily created but imagine we have once(). } public function get_instance(){ return $this->conn; } } class A2 extends Context2 {} class B2 extends A2 {} $a = new A(); $b = new B(); $a2 = new A2(); $b2 = new B2(); var_dump($a->get_instance()); var_dump($b->get_instance()); var_dump($a2->get_instance()); var_dump($b2->get_instance());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  (null)
number of ops:  34
compiled vars:  !0 = $a, !1 = $b, !2 = $a2, !3 = $b2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    1     0  E >   ECHO                                                     '%24value+%3D+null%3B+%24executed+%3D+false%3B+return+function+%28%29+use+%28%24producer%2C+%26+%24value%2C+%26+%24executed%29+%7B%0A%09%09%09if+%28%21%24executed%29+%7B%0A%09%09%09%09%24value+%3D+%24producer%28%29%3B%0A%09%09%09%09%24executed+%3D+true%3B%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return+%24value%3B%0A%09%09%7D%3B%0A%09%09%0A%09%09%0A%09%09%0A%09%09%0A'
   39     1        NEW                                              $4      'A'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   40     4        NEW                                              $7      'B'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $7
   41     7        NEW                                              $10     'A2'
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !2, $10
   42    10        NEW                                              $13     'B2'
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !3, $13
   44    13        INIT_FCALL                                               'var_dump'
         14        INIT_METHOD_CALL                                         !0, 'get_instance'
         15        DO_FCALL                                      0  $16     
         16        SEND_VAR                                                 $16
         17        DO_ICALL                                                 
   45    18        INIT_FCALL                                               'var_dump'
         19        INIT_METHOD_CALL                                         !1, 'get_instance'
         20        DO_FCALL                                      0  $18     
         21        SEND_VAR                                                 $18
         22        DO_ICALL                                                 
   46    23        INIT_FCALL                                               'var_dump'
         24        INIT_METHOD_CALL                                         !2, 'get_instance'
         25        DO_FCALL                                      0  $20     
         26        SEND_VAR                                                 $20
         27        DO_ICALL                                                 
   47    28        INIT_FCALL                                               'var_dump'
         29        INIT_METHOD_CALL                                         !3, 'get_instance'
         30        DO_FCALL                                      0  $22     
         31        SEND_VAR                                                 $22
         32        DO_ICALL                                                 
         33      > RETURN                                                   1

Class Connection: [no user functions]
Class Context:
Function get_instance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  get_instance
number of ops:  9
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   BIND_STATIC                                              !0
   19     1        JMP_SET                                          ~1      !0, ->6
          2        NEW                                              $2      'Connection'
          3        DO_FCALL                                      0          
          4        ASSIGN                                           ~4      !0, $2
          5        QM_ASSIGN                                        ~1      ~4
          6        FREE                                                     ~1
   20     7      > RETURN                                                   !0
   21     8*     > RETURN                                                   null

End of function get_instance

End of class Context.

Class A:
Function get_instance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  get_instance
number of ops:  9
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   BIND_STATIC                                              !0
   19     1        JMP_SET                                          ~1      !0, ->6
          2        NEW                                              $2      'Connection'
          3        DO_FCALL                                      0          
          4        ASSIGN                                           ~4      !0, $2
          5        QM_ASSIGN                                        ~1      ~4
          6        FREE                                                     ~1
   20     7      > RETURN                                                   !0
   21     8*     > RETURN                                                   null

End of function get_instance

End of class A.

Class B:
Function get_instance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  get_instance
number of ops:  9
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   BIND_STATIC                                              !0
   19     1        JMP_SET                                          ~1      !0, ->6
          2        NEW                                              $2      'Connection'
          3        DO_FCALL                                      0          
          4        ASSIGN                                           ~4      !0, $2
          5        QM_ASSIGN                                        ~1      ~4
          6        FREE                                                     ~1
   20     7      > RETURN                                                   !0
   21     8*     > RETURN                                                   null

End of function get_instance

End of class B.

Class Context2:
Function __constructor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  __constructor
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   NEW                                              $1      'Connection'
          1        DO_FCALL                                      0          
          2        ASSIGN_OBJ                                               'conn'
          3        OP_DATA                                                  $1
   30     4      > RETURN                                                   null

End of function __constructor

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

End of function get_instance

End of class Context2.

Class A2:
Function __constructor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  __constructor
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   NEW                                              $1      'Connection'
          1        DO_FCALL                                      0          
          2        ASSIGN_OBJ                                               'conn'
          3        OP_DATA                                                  $1
   30     4      > RETURN                                                   null

End of function __constructor

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

End of function get_instance

End of class A2.

Class B2:
Function __constructor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OYbWc
function name:  __constructor
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   NEW                                              $1      'Connection'
          1        DO_FCALL                                      0          
          2        ASSIGN_OBJ                                               'conn'
          3        OP_DATA                                                  $1
   30     4      > RETURN                                                   null

End of function __constructor

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

End of function get_instance

End of class B2.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.87 ms | 1408 KiB | 15 Q