3v4l.org

run code in 300+ 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 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QbJ4p
function name:  load
number of ops:  30
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        INIT_FCALL                                               'implode'
          4        SEND_VAL                                                 '%2C+'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $3      
          7        CONCAT                                           ~4      !0, $3
          8        SEND_VAL                                                 ~4
          9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !2, $5
    9    11        FETCH_STATIC_PROP_R          unknown             ~7      'instances'
         12        ARRAY_KEY_EXISTS                                         !2, ~7
         13      > JMPZ                                                     ~8, ->18
   10    14    >   ECHO                                                     'Hit%21'
   11    15        FETCH_STATIC_PROP_R          unknown             ~9      'instances'
         16        FETCH_DIM_R                                      ~10     ~9, !2
         17      > RETURN                                                   ~10
   14    18    >   FETCH_CLASS                                   0  $13     !0
         19        NEW                                              $14     $13
         20        SEND_UNPACK                                              !1
         21        CHECK_UNDEF_ARGS                                         
         22        DO_FCALL                                      1          
         23        FETCH_STATIC_PROP_W          unknown             $11     'instances'
         24        ASSIGN_DIM                                               $11, !2
         25        OP_DATA                                                  $14
   15    26        FETCH_STATIC_PROP_R          unknown             ~16     'instances'
         27        FETCH_DIM_R                                      ~17     ~16, !2
         28      > RETURN                                                   ~17
   16    29*     > 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.0.0


preferences:
152.77 ms | 1016 KiB | 15 Q