3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Singleton{ final public static function get_instance(...$args) { static $instance = []; $called_class = get_called_class(); if( !isset( $instance[ $called_class ] ) ) { $instance[$called_class] = new $called_class(...$args); } return $instance[ $called_class ]; } } class Database { use Singleton; protected function __construct(public readonly string $username, public readonly string $password) { } } Database::get_instance('admin', 'admin'); var_dump(Database::get_instance('readonly', 'readonly')); var_dump(Database::get_instance());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/43NIj
function name:  (null)
number of ops:  18
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   DECLARE_CLASS                                            'database'
   26     1        INIT_STATIC_METHOD_CALL                                  'Database', 'get_instance'
          2        SEND_VAL_EX                                              'admin'
          3        SEND_VAL_EX                                              'admin'
          4        DO_FCALL                                      0          
   27     5        INIT_FCALL                                               'var_dump'
          6        INIT_STATIC_METHOD_CALL                                  'Database', 'get_instance'
          7        SEND_VAL_EX                                              'readonly'
          8        SEND_VAL_EX                                              'readonly'
          9        DO_FCALL                                      0  $1      
         10        SEND_VAR                                                 $1
         11        DO_ICALL                                                 
   28    12        INIT_FCALL                                               'var_dump'
         13        INIT_STATIC_METHOD_CALL                                  'Database', 'get_instance'
         14        DO_FCALL                                      0  $3      
         15        SEND_VAR                                                 $3
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Class Singleton:
Function get_instance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 14
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/43NIj
function name:  get_instance
number of ops:  17
compiled vars:  !0 = $args, !1 = $instance, !2 = $called_class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV_VARIADIC                                    !0      
    7     1        BIND_STATIC                                              !1
    8     2        GET_CALLED_CLASS                                 ~3      
          3        ASSIGN                                                   !2, ~3
   10     4        ISSET_ISEMPTY_DIM_OBJ                         0  ~5      !1, !2
          5        BOOL_NOT                                         ~6      ~5
          6      > JMPZ                                                     ~6, ->14
   11     7    >   FETCH_CLASS                                   0  $8      !2
          8        NEW                                              $9      $8
          9        SEND_UNPACK                                              !0
         10        CHECK_UNDEF_ARGS                                         
         11        DO_FCALL                                      1          
         12        ASSIGN_DIM                                               !1, !2
         13        OP_DATA                                                  $9
   14    14    >   FETCH_DIM_R                                      ~11     !1, !2
         15      > RETURN                                                   ~11
   15    16*     > RETURN                                                   null

End of function get_instance

End of class Singleton.

Class Database:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/43NIj
function name:  __construct
number of ops:  7
compiled vars:  !0 = $username, !1 = $password
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        ASSIGN_OBJ                                               'username'
          3        OP_DATA                                                  !0
          4        ASSIGN_OBJ                                               'password'
          5        OP_DATA                                                  !1
   22     6      > RETURN                                                   null

End of function __construct

End of class Database.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.76 ms | 1015 KiB | 14 Q