3v4l.org

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


preferences:
157 ms | 1011 KiB | 15 Q