3v4l.org

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

Class SingleInstance:
Function load:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/tWUHJ
function name:  load
number of ops:  24
compiled vars:  !0 = $class, !1 = $args, !2 = $c
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      null
    9     2      > JMPZ                                                         !1, ->5
   10     3    >   FRAMELESS_ICALL_2                implode             ~3      '%2C+', !1
          4        ASSIGN                                                       !1, ~3
   13     5    >   FETCH_CLASS                                       0  $5      !0
          6        NEW                                                  $6      $5
          7        SEND_VAR_EX                                                  !1
          8        DO_FCALL                                          0          
          9        ASSIGN                                                       !2, $6
   14    10        FETCH_STATIC_PROP_R              unknown             ~9      'instances'
         11        FRAMELESS_ICALL_2                in_array            ~10     !2, ~9
         12      > JMPZ                                                         ~10, ->17
   15    13    >   ECHO                                                         'Hit%21'
   16    14        FETCH_STATIC_PROP_R              unknown             ~11     'instances'
         15        FETCH_DIM_R                                          ~12     ~11, !0
         16      > RETURN                                                       ~12
   19    17    >   FETCH_STATIC_PROP_W              unknown             $13     'instances'
         18        ASSIGN_DIM                                                   $13, !0
         19        OP_DATA                                                      !2
   20    20        FETCH_STATIC_PROP_R              unknown             ~15     'instances'
         21        FETCH_DIM_R                                          ~16     ~15, !0
         22      > RETURN                                                       ~16
   21    23*     > 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/tWUHJ
function name:  __construct
number of ops:  4
compiled vars:  !0 = $word
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
   29     1        ASSIGN_OBJ                                                   'word'
          2        OP_DATA                                                      !0
   30     3      > 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/tWUHJ
function name:  show
number of ops:  3
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   FETCH_OBJ_R                                          ~0      'word'
          1      > RETURN                                                       ~0
   34     2*     > RETURN                                                       null

End of function show

End of class Words.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
314.24 ms | 2528 KiB | 13 Q