3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Singleton { /** * @var Singleton[] The reference to *Singleton* instances of any child class. */ private static $instances = array(); /** * Returns the *Singleton* instance of this class. * * @return static The *Singleton* instance. */ public static function getInstance() { if (!isset(self::$instances[static::class])) { self::$instances[static::class] = new static(); } return self::$instances[static::class]; } /** * Protected constructor to prevent creating a new instance of the * *Singleton* via the `new` operator from outside of this class. */ protected function __construct() { } /** * Private clone method to prevent cloning of the instance of the * *Singleton* instance. * * @return void */ private function __clone() { } /** * Private unserialize method to prevent unserializing of the *Singleton* * instance. * * @return void */ private function __wakeup() { } } class SingletonChild extends Singleton { } class AnotherChild extends Singleton { } echo 'Expect: true, true, true, false' . PHP_EOL; var_dump(SingletonChild::getInstance() === SingletonChild::getInstance()); var_dump(AnotherChild::getInstance() === AnotherChild::getInstance()); $a = AnotherChild::getInstance(); var_dump($a === AnotherChild::getInstance()); var_dump($a === SingletonChild::getInstance());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4TN3d
function name:  (null)
number of ops:  33
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   ECHO                                                     'Expect%3A+true%2C+true%2C+true%2C+false%0A'
   63     1        INIT_FCALL                                               'var_dump'
          2        INIT_STATIC_METHOD_CALL                                  'SingletonChild', 'getInstance'
          3        DO_FCALL                                      0  $1      
          4        INIT_STATIC_METHOD_CALL                                  'SingletonChild', 'getInstance'
          5        DO_FCALL                                      0  $2      
          6        IS_IDENTICAL                                     ~3      $1, $2
          7        SEND_VAL                                                 ~3
          8        DO_ICALL                                                 
   64     9        INIT_FCALL                                               'var_dump'
         10        INIT_STATIC_METHOD_CALL                                  'AnotherChild', 'getInstance'
         11        DO_FCALL                                      0  $5      
         12        INIT_STATIC_METHOD_CALL                                  'AnotherChild', 'getInstance'
         13        DO_FCALL                                      0  $6      
         14        IS_IDENTICAL                                     ~7      $5, $6
         15        SEND_VAL                                                 ~7
         16        DO_ICALL                                                 
   66    17        INIT_STATIC_METHOD_CALL                                  'AnotherChild', 'getInstance'
         18        DO_FCALL                                      0  $9      
         19        ASSIGN                                                   !0, $9
   67    20        INIT_FCALL                                               'var_dump'
         21        INIT_STATIC_METHOD_CALL                                  'AnotherChild', 'getInstance'
         22        DO_FCALL                                      0  $11     
         23        IS_IDENTICAL                                     ~12     !0, $11
         24        SEND_VAL                                                 ~12
         25        DO_ICALL                                                 
   68    26        INIT_FCALL                                               'var_dump'
         27        INIT_STATIC_METHOD_CALL                                  'SingletonChild', 'getInstance'
         28        DO_FCALL                                      0  $14     
         29        IS_IDENTICAL                                     ~15     !0, $14
         30        SEND_VAL                                                 ~15
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Class Singleton:
Function getinstance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/4TN3d
function name:  getInstance
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   FETCH_CLASS_NAME                                 ~1      
          1        FETCH_STATIC_PROP_IS                             ~0      'instances'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~0, ~1
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->11
   18     5    >   FETCH_CLASS_NAME                                 ~5      
          6        NEW                          static              $7      
          7        DO_FCALL                                      0          
          8        FETCH_STATIC_PROP_W          unknown             $4      'instances'
          9        ASSIGN_DIM                                               $4, ~5
         10        OP_DATA                                                  $7
   21    11    >   FETCH_CLASS_NAME                                 ~10     
         12        FETCH_STATIC_PROP_R          unknown             ~9      'instances'
         13        FETCH_DIM_R                                      ~11     ~9, ~10
         14      > RETURN                                                   ~11
   22    15*     > RETURN                                                   null

End of function getinstance

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

End of function __construct

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4TN3d
function name:  __clone
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E > > 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/4TN3d
function name:  __wakeup
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   null

End of function __wakeup

End of class Singleton.

Class SingletonChild:
Function getinstance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/4TN3d
function name:  getInstance
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   FETCH_CLASS_NAME                                 ~1      
          1        FETCH_STATIC_PROP_IS                             ~0      'instances'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~0, ~1
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->11
   18     5    >   FETCH_CLASS_NAME                                 ~5      
          6        NEW                          static              $7      
          7        DO_FCALL                                      0          
          8        FETCH_STATIC_PROP_W          unknown             $4      'instances'
          9        ASSIGN_DIM                                               $4, ~5
         10        OP_DATA                                                  $7
   21    11    >   FETCH_CLASS_NAME                                 ~10     
         12        FETCH_STATIC_PROP_R          unknown             ~9      'instances'
         13        FETCH_DIM_R                                      ~11     ~9, ~10
         14      > RETURN                                                   ~11
   22    15*     > RETURN                                                   null

End of function getinstance

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

End of function __construct

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4TN3d
function name:  __clone
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E > > 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/4TN3d
function name:  __wakeup
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   null

End of function __wakeup

End of class SingletonChild.

Class AnotherChild:
Function getinstance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/4TN3d
function name:  getInstance
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   FETCH_CLASS_NAME                                 ~1      
          1        FETCH_STATIC_PROP_IS                             ~0      'instances'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~0, ~1
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->11
   18     5    >   FETCH_CLASS_NAME                                 ~5      
          6        NEW                          static              $7      
          7        DO_FCALL                                      0          
          8        FETCH_STATIC_PROP_W          unknown             $4      'instances'
          9        ASSIGN_DIM                                               $4, ~5
         10        OP_DATA                                                  $7
   21    11    >   FETCH_CLASS_NAME                                 ~10     
         12        FETCH_STATIC_PROP_R          unknown             ~9      'instances'
         13        FETCH_DIM_R                                      ~11     ~9, ~10
         14      > RETURN                                                   ~11
   22    15*     > RETURN                                                   null

End of function getinstance

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

End of function __construct

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4TN3d
function name:  __clone
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E > > 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/4TN3d
function name:  __wakeup
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   null

End of function __wakeup

End of class AnotherChild.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.84 ms | 1023 KiB | 14 Q