3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class SingletonUtil { /** * @var mixed[] * Format: $[$key] =& $variable */ private static $variables = []; /** * @var mixed[] */ private static $defaults = []; /** * @param string $key * @param mixed $variable * @param mixed $value * @param mixed|null $default * Default value to be used when resetting. * * @return mixed */ public static function init($key, &$variable, $value, $default = NULL) { self::$variables[$key] =& $variable; self::$defaults[$key] = $default; return $variable = $value; } /** * @param string ($key_or_null */ public static function reset($key_or_null = NULL) { if (NULL === $key_or_null) { foreach (self::$variables as $key => $variable) { self::$variables[$key_or_null] = self::$defaults[$key_or_null]; } } elseif (array_key_exists($key_or_null, self::$variables)) { self::$variables[$key_or_null] = self::$defaults[$key_or_null]; } } } class C { function __construct() { print __METHOD__ . "()\n"; } } /** * @return mixed */ function singleton_test() { // This would otherwise use drupal_static_fast pattern. // But this is even faster. static $cache; return $cache ?: SingletonUtil::init( __FUNCTION__, $cache, new C()); } function assert_or_ok($boolval) { if (!$boolval) { assert(FALSE); exit(); } print "ok\n"; } $obj = singleton_test(); assert_or_ok($obj instanceof C); assert_or_ok($obj === singleton_test()); SingletonUtil::reset('singleton_test'); assert_or_ok($obj !== singleton_test()); $obj = singleton_test(); assert_or_ok($obj instanceof C); assert_or_ok($obj === singleton_test()); SingletonUtil::reset('singleton_test'); assert_or_ok($obj !== singleton_test());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZqHdj
function name:  (null)
number of ops:  45
compiled vars:  !0 = $obj
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   INIT_FCALL                                               'singleton_test'
          1        DO_FCALL                                      0  $1      
          2        ASSIGN                                                   !0, $1
   76     3        INIT_FCALL                                               'assert_or_ok'
          4        INSTANCEOF                                       ~3      !0, 'C'
          5        SEND_VAL                                                 ~3
          6        DO_FCALL                                      0          
   77     7        INIT_FCALL                                               'assert_or_ok'
          8        INIT_FCALL                                               'singleton_test'
          9        DO_FCALL                                      0  $5      
         10        IS_IDENTICAL                                     ~6      !0, $5
         11        SEND_VAL                                                 ~6
         12        DO_FCALL                                      0          
   79    13        INIT_STATIC_METHOD_CALL                                  'SingletonUtil', 'reset'
         14        SEND_VAL                                                 'singleton_test'
         15        DO_FCALL                                      0          
   80    16        INIT_FCALL                                               'assert_or_ok'
         17        INIT_FCALL                                               'singleton_test'
         18        DO_FCALL                                      0  $9      
         19        IS_NOT_IDENTICAL                                 ~10     !0, $9
         20        SEND_VAL                                                 ~10
         21        DO_FCALL                                      0          
   82    22        INIT_FCALL                                               'singleton_test'
         23        DO_FCALL                                      0  $12     
         24        ASSIGN                                                   !0, $12
   83    25        INIT_FCALL                                               'assert_or_ok'
         26        INSTANCEOF                                       ~14     !0, 'C'
         27        SEND_VAL                                                 ~14
         28        DO_FCALL                                      0          
   84    29        INIT_FCALL                                               'assert_or_ok'
         30        INIT_FCALL                                               'singleton_test'
         31        DO_FCALL                                      0  $16     
         32        IS_IDENTICAL                                     ~17     !0, $16
         33        SEND_VAL                                                 ~17
         34        DO_FCALL                                      0          
   86    35        INIT_STATIC_METHOD_CALL                                  'SingletonUtil', 'reset'
         36        SEND_VAL                                                 'singleton_test'
         37        DO_FCALL                                      0          
   87    38        INIT_FCALL                                               'assert_or_ok'
         39        INIT_FCALL                                               'singleton_test'
         40        DO_FCALL                                      0  $20     
         41        IS_NOT_IDENTICAL                                 ~21     !0, $20
         42        SEND_VAL                                                 ~21
         43        DO_FCALL                                      0          
         44      > RETURN                                                   1

Function singleton_test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZqHdj
function name:  singleton_test
number of ops:  12
compiled vars:  !0 = $cache
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   BIND_STATIC                                              !0
   60     1        JMP_SET                                          ~1      !0, ->10
   61     2        INIT_STATIC_METHOD_CALL                                  'SingletonUtil', 'init'
   62     3        SEND_VAL                                                 'singleton_test'
   63     4        SEND_REF                                                 !0
   64     5        NEW                                              $2      'C'
          6        DO_FCALL                                      0          
          7        SEND_VAR                                                 $2
          8        DO_FCALL                                      0  $4      
          9        QM_ASSIGN                                        ~1      $4
         10      > RETURN                                                   ~1
   65    11*     > RETURN                                                   null

End of function singleton_test

Function assert_or_ok:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZqHdj
function name:  assert_or_ok
number of ops:  11
compiled vars:  !0 = $boolval
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
   68     1        BOOL_NOT                                         ~1      !0
          2      > JMPZ                                                     ~1, ->9
   69     3    >   ASSERT_CHECK                                             
          4        INIT_FCALL                                               'assert'
          5        SEND_VAL                                                 <false>
          6        SEND_VAL                                                 'assert%28FALSE%29'
          7        DO_ICALL                                                 
   70     8      > EXIT                                                     
   72     9    >   ECHO                                                     'ok%0A'
   73    10      > RETURN                                                   null

End of function assert_or_ok

Class SingletonUtil:
Function init:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZqHdj
function name:  init
number of ops:  13
compiled vars:  !0 = $key, !1 = $variable, !2 = $value, !3 = $default
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      null
   26     4        FETCH_STATIC_PROP_W          global              $4      'variables'
          5        FETCH_DIM_W                                      $5      $4, !0
          6        ASSIGN_REF                                               $5, !1
   27     7        FETCH_STATIC_PROP_W          unknown             $7      'defaults'
          8        ASSIGN_DIM                                               $7, !0
          9        OP_DATA                                                  !3
   28    10        ASSIGN                                           ~9      !1, !2
         11      > RETURN                                                   ~9
   29    12*     > RETURN                                                   null

End of function init

Function reset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/ZqHdj
function name:  reset
number of ops:  24
compiled vars:  !0 = $key_or_null, !1 = $variable, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV_INIT                                        !0      null
   35     1        TYPE_CHECK                                    2          !0
          2      > JMPZ                                                     ~3, ->15
   36     3    >   FETCH_STATIC_PROP_R          unknown             ~4      'variables'
          4      > FE_RESET_R                                       $5      ~4, ->13
          5    > > FE_FETCH_R                                       ~6      $5, !1, ->13
          6    >   ASSIGN                                                   !2, ~6
   37     7        FETCH_STATIC_PROP_R          unknown             ~10     'defaults'
          8        FETCH_DIM_R                                      ~11     ~10, !0
          9        FETCH_STATIC_PROP_W          unknown             $8      'variables'
         10        ASSIGN_DIM                                               $8, !0
         11        OP_DATA                                                  ~11
   36    12      > JMP                                                      ->5
         13    >   FE_FREE                                                  $5
         14      > JMP                                                      ->23
   40    15    >   FETCH_STATIC_PROP_R          unknown             ~12     'variables'
         16        ARRAY_KEY_EXISTS                                         !0, ~12
         17      > JMPZ                                                     ~13, ->23
   41    18    >   FETCH_STATIC_PROP_R          unknown             ~16     'defaults'
         19        FETCH_DIM_R                                      ~17     ~16, !0
         20        FETCH_STATIC_PROP_W          unknown             $14     'variables'
         21        ASSIGN_DIM                                               $14, !0
         22        OP_DATA                                                  ~17
   43    23    > > RETURN                                                   null

End of function reset

End of class SingletonUtil.

Class C:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZqHdj
function name:  __construct
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   ECHO                                                     'C%3A%3A__construct%28%29%0A'
   50     1      > RETURN                                                   null

End of function __construct

End of class C.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.69 ms | 1411 KiB | 27 Q