3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class DbConnection { private $dsn; private $initializer; public function __construct(string $dsn) { $this->dsn = $dsn; // socket stuff happens here, much like with PDO } public function query(string $queryString) : array { ($this->initializer)(); // irrelevant from here on return ['query' => $queryString, 'dsn' => $this->dsn]; } public static function lazyInstance(string $dsn) : self { $instance = (new ReflectionClass(self::class))->newInstanceWithoutConstructor(); $instance->initializer = function () use ($dsn, $instance) { $instance->__construct($dsn); $instance->initializer = function () { }; }; return $instance; } } $instance = DbConnection::lazyInstance('mysql://something'); var_dump($instance); var_dump($instance->query('SELECT * FROM foo')); var_dump($instance->query('SELECT * FROM bar'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y0eoL
function name:  (null)
number of ops:  20
compiled vars:  !0 = $instance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   INIT_STATIC_METHOD_CALL                                  'DbConnection', 'lazyInstance'
          1        SEND_VAL                                                 'mysql%3A%2F%2Fsomething'
          2        DO_FCALL                                      0  $1      
          3        ASSIGN                                                   !0, $1
   35     4        INIT_FCALL                                               'var_dump'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
   37     7        INIT_FCALL                                               'var_dump'
          8        INIT_METHOD_CALL                                         !0, 'query'
          9        SEND_VAL_EX                                              'SELECT+%2A+FROM+foo'
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR                                                 $4
         12        DO_ICALL                                                 
   38    13        INIT_FCALL                                               'var_dump'
         14        INIT_METHOD_CALL                                         !0, 'query'
         15        SEND_VAL_EX                                              'SELECT+%2A+FROM+bar'
         16        DO_FCALL                                      0  $6      
         17        SEND_VAR                                                 $6
         18        DO_ICALL                                                 
         19      > RETURN                                                   1

Class DbConnection:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y0eoL
function name:  __construct
number of ops:  4
compiled vars:  !0 = $dsn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        ASSIGN_OBJ                                               'dsn'
          2        OP_DATA                                                  !0
   12     3      > RETURN                                                   null

End of function __construct

Function query:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y0eoL
function name:  query
number of ops:  11
compiled vars:  !0 = $queryString
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   16     1        FETCH_OBJ_R                                      ~1      'initializer'
          2        INIT_DYNAMIC_CALL                                        ~1
          3        DO_FCALL                                      0          
   18     4        INIT_ARRAY                                       ~3      !0, 'query'
          5        FETCH_OBJ_R                                      ~4      'dsn'
          6        ADD_ARRAY_ELEMENT                                ~3      ~4, 'dsn'
          7        VERIFY_RETURN_TYPE                                       ~3
          8      > RETURN                                                   ~3
   19     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function query

Function lazyinstance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y0eoL
function name:  lazyInstance
number of ops:  16
compiled vars:  !0 = $dsn, !1 = $instance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        NEW                                              $2      'ReflectionClass'
          2        SEND_VAL_EX                                              'DbConnection'
          3        DO_FCALL                                      0          
          4        INIT_METHOD_CALL                                         $2, 'newInstanceWithoutConstructor'
          5        DO_FCALL                                      0  $4      
          6        ASSIGN                                                   !1, $4
   24     7        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
          8        BIND_LEXICAL                                             ~7, !0
          9        BIND_LEXICAL                                             ~7, !1
         10        ASSIGN_OBJ                                               !1, 'initializer'
   28    11        OP_DATA                                                  ~7
   29    12        VERIFY_RETURN_TYPE                                       !1
         13      > RETURN                                                   !1
   30    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y0eoL
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $dsn, !1 = $instance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   25     2        INIT_METHOD_CALL                                         !1, '__construct'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   26     5        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          6        ASSIGN_OBJ                                               !1, 'initializer'
   27     7        OP_DATA                                                  ~4
   28     8      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y0eoL
function name:  {closure}
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E > > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 0

End of function lazyinstance

End of class DbConnection.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.63 ms | 1450 KiB | 14 Q