3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait singleton { /** * private construct, generally defined by using class */ //private function __construct() {} public static function getInstance() { static $_instance = NULL; $class = __CLASS__; return $_instance ?: $_instance = new $class; } public function __clone() { trigger_error('Cloning '.__CLASS__.' is not allowed.',E_USER_ERROR); } public function __wakeup() { trigger_error('Unserializing '.__CLASS__.' is not allowed.',E_USER_ERROR); } } /** * Example Usage */ class foo { use singleton; private function __construct() { $this->name = 'foo'; } } class bar { use singleton; private function __construct() { $this->name = 'bar'; } } $foo = foo::getInstance(); echo $foo->name; $bar = bar::getInstance(); echo $bar->name;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/16GIC
function name:  (null)
number of ops:  13
compiled vars:  !0 = $foo, !1 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   DECLARE_CLASS                                            'foo'
   36     1        DECLARE_CLASS                                            'bar'
   44     2        INIT_STATIC_METHOD_CALL                                  'foo', 'getInstance'
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !0, $2
   45     5        FETCH_OBJ_R                                      ~4      !0, 'name'
          6        ECHO                                                     ~4
   47     7        INIT_STATIC_METHOD_CALL                                  'bar', 'getInstance'
          8        DO_FCALL                                      0  $5      
          9        ASSIGN                                                   !1, $5
   48    10        FETCH_OBJ_R                                      ~7      !1, 'name'
         11        ECHO                                                     ~7
         12      > RETURN                                                   1

Class singleton:
Function getinstance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/16GIC
function name:  getInstance
number of ops:  11
compiled vars:  !0 = $_instance, !1 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   BIND_STATIC                                              !0
   11     1        FETCH_CLASS_NAME                                 ~2      
          2        ASSIGN                                                   !1, ~2
   12     3        JMP_SET                                          ~4      !0, ->9
          4        FETCH_CLASS                                   0  $5      !1
          5        NEW                                              $6      $5
          6        DO_FCALL                                      0          
          7        ASSIGN                                           ~8      !0, $6
          8        QM_ASSIGN                                        ~4      ~8
          9      > RETURN                                                   ~4
   13    10*     > RETURN                                                   null

End of function getinstance

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/16GIC
function name:  __clone
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   INIT_FCALL                                               'trigger_error'
          1        FETCH_CLASS_NAME                                 ~0      
          2        CONCAT                                           ~1      'Cloning+', ~0
          3        CONCAT                                           ~2      ~1, '+is+not+allowed.'
          4        SEND_VAL                                                 ~2
          5        SEND_VAL                                                 256
          6        DO_ICALL                                                 
   17     7      > RETURN                                                   null

End of function __clone

Function __wakeup:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/16GIC
function name:  __wakeup
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   INIT_FCALL                                               'trigger_error'
          1        FETCH_CLASS_NAME                                 ~0      
          2        CONCAT                                           ~1      'Unserializing+', ~0
          3        CONCAT                                           ~2      ~1, '+is+not+allowed.'
          4        SEND_VAL                                                 ~2
          5        SEND_VAL                                                 256
          6        DO_ICALL                                                 
   21     7      > RETURN                                                   null

End of function __wakeup

End of class singleton.

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

End of function __construct

End of class foo.

Class bar:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/16GIC
function name:  __construct
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   ASSIGN_OBJ                                               'name'
          1        OP_DATA                                                  'bar'
   41     2      > RETURN                                                   null

End of function __construct

End of class bar.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.27 ms | 1400 KiB | 15 Q