3v4l.org

run code in 500+ PHP versions simultaneously
<?php class SingleInstance { private static $instances = []; public static function load($class, $args = null) { $checksum = md5($class.implode(', ', $args)); if (array_key_exists($checksum, self::$instances)) { echo "Hit!"; return self::$instances[$checksum]; } self::$instances[$checksum] = new $class(... $args); return self::$instances[$checksum]; } } class Words { private $word; private $word2; public function __construct($word, $word2) { $this->word = $word; $this->word2 = $word2; } public function show() { return $this->word." ".$this->word2; } } $a = SingleInstance::load('Words', ['Dog', 'Word1']); echo $a->show().PHP_EOL; $b = SingleInstance::load('Words', ['Cat', 'Word2']); echo $b->show().PHP_EOL; $c = SingleInstance::load('Words', ['Cat', 'Word2']); echo $c->show().PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QbJ4p
function name:  (null)
number of ops:  28
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   34     0  E >   INIT_STATIC_METHOD_CALL                                      'SingleInstance', 'load'
          1        SEND_VAL                                                     'Words'
          2        SEND_VAL                                                     <array>
          3        DO_FCALL                                          0  $3      
          4        ASSIGN                                                       !0, $3
   35     5        INIT_METHOD_CALL                                             !0, 'show'
          6        DO_FCALL                                          0  $5      
          7        CONCAT                                               ~6      $5, '%0A'
          8        ECHO                                                         ~6
   37     9        INIT_STATIC_METHOD_CALL                                      'SingleInstance', 'load'
         10        SEND_VAL                                                     'Words'
         11        SEND_VAL                                                     <array>
         12        DO_FCALL                                          0  $7      
         13        ASSIGN                                                       !1, $7
   38    14        INIT_METHOD_CALL                                             !1, 'show'
         15        DO_FCALL                                          0  $9      
         16        CONCAT                                               ~10     $9, '%0A'
         17        ECHO                                                         ~10
   40    18        INIT_STATIC_METHOD_CALL                                      'SingleInstance', 'load'
         19        SEND_VAL                                                     'Words'
         20        SEND_VAL                                                     <array>
         21        DO_FCALL                                          0  $11     
         22        ASSIGN                                                       !2, $11
   41    23        INIT_METHOD_CALL                                             !2, 'show'
         24        DO_FCALL                                          0  $13     
         25        CONCAT                                               ~14     $13, '%0A'
         26        ECHO                                                         ~14
   42    27      > RETURN                                                       1

Class SingleInstance:
Function load:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QbJ4p
function name:  load
number of ops:  27
compiled vars:  !0 = $class, !1 = $args, !2 = $checksum
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    6     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      null
    8     2        INIT_FCALL                                                   'md5'
          3        FRAMELESS_ICALL_2                implode             ~3      '%2C+', !1
          4        CONCAT                                               ~4      !0, ~3
          5        SEND_VAL                                                     ~4
          6        DO_ICALL                                             $5      
          7        ASSIGN                                                       !2, $5
    9     8        FETCH_STATIC_PROP_R              global lock         ~7      'instances'
          9        ARRAY_KEY_EXISTS                                             !2, ~7
         10      > JMPZ                                                         ~8, ->15
   10    11    >   ECHO                                                         'Hit%21'
   11    12        FETCH_STATIC_PROP_R              unknown             ~9      'instances'
         13        FETCH_DIM_R                                          ~10     ~9, !2
         14      > RETURN                                                       ~10
   14    15    >   FETCH_CLASS                                       0  $13     !0
         16        NEW                                                  $14     $13
         17        SEND_UNPACK                                                  !1
         18        CHECK_UNDEF_ARGS                                             
         19        DO_FCALL                                          1          
         20        FETCH_STATIC_PROP_W              unknown             $11     'instances'
         21        ASSIGN_DIM                                                   $11, !2
         22        OP_DATA                                                      $14
   15    23        FETCH_STATIC_PROP_R              unknown             ~16     'instances'
         24        FETCH_DIM_R                                          ~17     ~16, !2
         25      > RETURN                                                       ~17
   16    26*     > RETURN                                                       null

End of function load

End of class SingleInstance.

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

End of function __construct

Function show:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QbJ4p
function name:  show
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E >   FETCH_OBJ_R                                          ~0      'word'
          1        CONCAT                                               ~1      ~0, '+'
          2        FETCH_OBJ_R                                          ~2      'word2'
          3        CONCAT                                               ~3      ~1, ~2
          4      > RETURN                                                       ~3
   31     5*     > RETURN                                                       null

End of function show

End of class Words.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
192.45 ms | 2006 KiB | 14 Q