3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Instantiator { public $dependencies = []; public function instantiate($type) { return $this->instantiateInternal($type, []); } private function instantiateInternal($type, array $stack) { if (in_array($type, $stack)) { throw new Exception(vsprintf('Cycle detected for `%s` as %s', [ $type, print_r($stack, true), ])); } $stack[] = $type; $graph = []; if (isset($this->dependencies[$type])) { foreach ($this->dependencies[$type] as $dependentType) { $graph[$dependentType] = $this->instantiateInternal($dependentType, $stack); } } return $graph; } } $i1 = new Instantiator(); $i1->dependencies = [ 'A' => ['B', 'C'], 'B' => ['D'], 'C' => ['D'], 'D' => ['E'], ]; var_dump($i1->instantiate('A')); $i2 = new Instantiator(); $i2->dependencies = [ 'A' => ['B', 'C'], 'B' => ['D'], 'C' => ['D'], 'D' => ['A'], ]; var_dump($i2->instantiate('A'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tGMQQ
function name:  (null)
number of ops:  23
compiled vars:  !0 = $i1, !1 = $i2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   NEW                                              $2      'Instantiator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   38     3        ASSIGN_OBJ                                               !0, 'dependencies'
   39     4        OP_DATA                                                  <array>
   44     5        INIT_FCALL                                               'var_dump'
          6        INIT_METHOD_CALL                                         !0, 'instantiate'
          7        SEND_VAL_EX                                              'A'
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_ICALL                                                 
   47    11        NEW                                              $8      'Instantiator'
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !1, $8
   49    14        ASSIGN_OBJ                                               !1, 'dependencies'
   50    15        OP_DATA                                                  <array>
   55    16        INIT_FCALL                                               'var_dump'
         17        INIT_METHOD_CALL                                         !1, 'instantiate'
         18        SEND_VAL_EX                                              'A'
         19        DO_FCALL                                      0  $12     
         20        SEND_VAR                                                 $12
         21        DO_ICALL                                                 
         22      > RETURN                                                   1

Class Instantiator:
Function instantiate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tGMQQ
function name:  instantiate
number of ops:  7
compiled vars:  !0 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        INIT_METHOD_CALL                                         'instantiateInternal'
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
   11     6*     > RETURN                                                   null

End of function instantiate

Function instantiateinternal:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 21
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 39
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 38
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 38
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 39
filename:       /in/tGMQQ
function name:  instantiateInternal
number of ops:  41
compiled vars:  !0 = $type, !1 = $stack, !2 = $graph, !3 = $dependentType
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        INIT_FCALL                                               'in_array'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $4      
          6      > JMPZ                                                     $4, ->21
   17     7    >   NEW                                              $5      'Exception'
          8        INIT_FCALL                                               'vsprintf'
          9        SEND_VAL                                                 'Cycle+detected+for+%60%25s%60+as+%25s'
   18    10        INIT_ARRAY                                       ~6      !0
   19    11        INIT_FCALL                                               'print_r'
         12        SEND_VAR                                                 !1
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $7      
         15        ADD_ARRAY_ELEMENT                                ~6      $7
         16        SEND_VAL                                                 ~6
         17        DO_ICALL                                         $8      
         18        SEND_VAR_NO_REF_EX                                       $8
         19        DO_FCALL                                      0          
         20      > THROW                                         0          $5
   22    21    >   ASSIGN_DIM                                               !1
         22        OP_DATA                                                  !0
   23    23        ASSIGN                                                   !2, <array>
   24    24        FETCH_OBJ_IS                                     ~12     'dependencies'
         25        ISSET_ISEMPTY_DIM_OBJ                         0          ~12, !0
         26      > JMPZ                                                     ~13, ->39
   26    27    >   FETCH_OBJ_R                                      ~14     'dependencies'
         28        FETCH_DIM_R                                      ~15     ~14, !0
         29      > FE_RESET_R                                       $16     ~15, ->38
         30    > > FE_FETCH_R                                               $16, !3, ->38
   28    31    >   INIT_METHOD_CALL                                         'instantiateInternal'
         32        SEND_VAR                                                 !3
         33        SEND_VAR                                                 !1
         34        DO_FCALL                                      0  $18     
         35        ASSIGN_DIM                                               !2, !3
         36        OP_DATA                                                  $18
   26    37      > JMP                                                      ->30
         38    >   FE_FREE                                                  $16
   31    39    > > RETURN                                                   !2
   32    40*     > RETURN                                                   null

End of function instantiateinternal

End of class Instantiator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.14 ms | 1400 KiB | 21 Q