3v4l.org

run code in 300+ PHP versions simultaneously
<?php class locator { public $instances; public function setDef($name, $instance){ $this->instances[$name] = $instance; } public function get($name){ if(isset($this->instances[$name])){ $o = (new ReflectionClass($name))->getConstructor(); if($o === null || null === $o->getParameters()){ return new $this->instances[$name](); } else { $p = $o->getParameters(); $instancesArray = []; foreach($p as $i){ $instancesArray[] = $this->get($i->getClass()->name); } $inst = new ReflectionClass($this->instances[$name]); return $inst->newInstanceArgs($instancesArray); } } } } class z { public $z = "zzzzzz"; } class base { public $b = "base !!!"; } class second { public $z; public $s = "second !!!"; public function __construct(z $a){ $this->z = $a; } } class a { public $base; public $second; public function __construct(base $b, second $s){ $this->base = $b; $this->second = $s; } } $loc = new locator(); $loc->setDef('z', z::class); $loc->setDef('second', second::class); $loc->setDef('base', base::class); $loc->setDef('a', a::class); $o = $loc->get('a'); echo $o->base->b; echo "<br>"; echo $o->second->s; echo "<br>"; echo $o->second->z->z; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VgRC1
function name:  (null)
number of ops:  36
compiled vars:  !0 = $loc, !1 = $o
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   NEW                                              $2      'locator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   53     3        INIT_METHOD_CALL                                         !0, 'setDef'
          4        SEND_VAL_EX                                              'z'
          5        SEND_VAL_EX                                              'z'
          6        DO_FCALL                                      0          
   54     7        INIT_METHOD_CALL                                         !0, 'setDef'
          8        SEND_VAL_EX                                              'second'
          9        SEND_VAL_EX                                              'second'
         10        DO_FCALL                                      0          
   55    11        INIT_METHOD_CALL                                         !0, 'setDef'
         12        SEND_VAL_EX                                              'base'
         13        SEND_VAL_EX                                              'base'
         14        DO_FCALL                                      0          
   56    15        INIT_METHOD_CALL                                         !0, 'setDef'
         16        SEND_VAL_EX                                              'a'
         17        SEND_VAL_EX                                              'a'
         18        DO_FCALL                                      0          
   58    19        INIT_METHOD_CALL                                         !0, 'get'
         20        SEND_VAL_EX                                              'a'
         21        DO_FCALL                                      0  $9      
         22        ASSIGN                                                   !1, $9
   59    23        FETCH_OBJ_R                                      ~11     !1, 'base'
         24        FETCH_OBJ_R                                      ~12     ~11, 'b'
         25        ECHO                                                     ~12
   60    26        ECHO                                                     '%3Cbr%3E'
   61    27        FETCH_OBJ_R                                      ~13     !1, 'second'
         28        FETCH_OBJ_R                                      ~14     ~13, 's'
         29        ECHO                                                     ~14
   62    30        ECHO                                                     '%3Cbr%3E'
   63    31        FETCH_OBJ_R                                      ~15     !1, 'second'
         32        FETCH_OBJ_R                                      ~16     ~15, 'z'
         33        FETCH_OBJ_R                                      ~17     ~16, 'z'
         34        ECHO                                                     ~17
   65    35      > RETURN                                                   1

Class locator:
Function setdef:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VgRC1
function name:  setDef
number of ops:  6
compiled vars:  !0 = $name, !1 = $instance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        FETCH_OBJ_W                                      $2      'instances'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
    6     5      > RETURN                                                   null

End of function setdef

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 53
Branch analysis from position: 4
2 jumps found. (Code = 47) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 41
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 41
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 16
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VgRC1
function name:  get
number of ops:  54
compiled vars:  !0 = $name, !1 = $o, !2 = $p, !3 = $instancesArray, !4 = $i, !5 = $inst
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        FETCH_OBJ_IS                                     ~6      'instances'
          2        ISSET_ISEMPTY_DIM_OBJ                         0          ~6, !0
          3      > JMPZ                                                     ~7, ->53
    9     4    >   NEW                                              $8      'ReflectionClass'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7        INIT_METHOD_CALL                                         $8, 'getConstructor'
          8        DO_FCALL                                      0  $10     
          9        ASSIGN                                                   !1, $10
   10    10        TYPE_CHECK                                    2  ~12     !1
         11      > JMPNZ_EX                                         ~12     ~12, ->16
         12    >   INIT_METHOD_CALL                                         !1, 'getParameters'
         13        DO_FCALL                                      0  $13     
         14        TYPE_CHECK                                    2  ~14     $13
         15        BOOL                                             ~12     ~14
         16    > > JMPZ                                                     ~12, ->24
   11    17    >   FETCH_OBJ_R                                      ~15     'instances'
         18        FETCH_DIM_R                                      ~16     ~15, !0
         19        FETCH_CLASS                                   0  $17     ~16
         20        NEW                                              $18     $17
         21        DO_FCALL                                      0          
         22      > RETURN                                                   $18
         23*       JMP                                                      ->53
   13    24    >   INIT_METHOD_CALL                                         !1, 'getParameters'
         25        DO_FCALL                                      0  $20     
         26        ASSIGN                                                   !2, $20
   14    27        ASSIGN                                                   !3, <array>
   15    28      > FE_RESET_R                                       $23     !2, ->41
         29    > > FE_FETCH_R                                               $23, !4, ->41
   16    30    >   INIT_METHOD_CALL                                         'get'
         31        CHECK_FUNC_ARG                                           
         32        INIT_METHOD_CALL                                         !4, 'getClass'
         33        DO_FCALL                                      0  $25     
         34        SEPARATE                                         $25     $25
         35        FETCH_OBJ_FUNC_ARG                               $26     $25, 'name'
         36        SEND_FUNC_ARG                                            $26
         37        DO_FCALL                                      0  $27     
         38        ASSIGN_DIM                                               !3
         39        OP_DATA                                                  $27
   15    40      > JMP                                                      ->29
         41    >   FE_FREE                                                  $23
   18    42        NEW                                              $28     'ReflectionClass'
         43        CHECK_FUNC_ARG                                           
         44        FETCH_OBJ_FUNC_ARG                               $29     'instances'
         45        FETCH_DIM_FUNC_ARG                               $30     $29, !0
         46        SEND_FUNC_ARG                                            $30
         47        DO_FCALL                                      0          
         48        ASSIGN                                                   !5, $28
   19    49        INIT_METHOD_CALL                                         !5, 'newInstanceArgs'
         50        SEND_VAR_EX                                              !3
         51        DO_FCALL                                      0  $33     
         52      > RETURN                                                   $33
   22    53    > > RETURN                                                   null

End of function get

End of class locator.

Class z: [no user functions]
Class base: [no user functions]
Class second:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VgRC1
function name:  __construct
number of ops:  4
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        ASSIGN_OBJ                                               'z'
          2        OP_DATA                                                  !0
   38     3      > RETURN                                                   null

End of function __construct

End of class second.

Class a:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VgRC1
function name:  __construct
number of ops:  7
compiled vars:  !0 = $b, !1 = $s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   45     2        ASSIGN_OBJ                                               'base'
          3        OP_DATA                                                  !0
   46     4        ASSIGN_OBJ                                               'second'
          5        OP_DATA                                                  !1
   47     6      > RETURN                                                   null

End of function __construct

End of class a.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.76 ms | 1407 KiB | 13 Q