3v4l.org

run code in 300+ PHP versions simultaneously
<?php // The smallest, ugliest autowiring container. function dep_set($n, $v) { $GLOBALS['deps'][$n] = $v; } function dep_get($n) { $v = &$GLOBALS['deps'][$n]; if (!$v) $v = $n; $r = $v instanceof Closure ? new ReflectionFunction($v) : (new ReflectionClass($n))->getConstructor(); $a = []; foreach ($r->getParameters() as $i => $p) { $pc = $p->getClass(); if (!$pc) throw new Exception('Cannot magically resolve a param $' . $p->name . ' without class typehint'); $pn = $pc->name; $a[] = dep_get(isset($GLOBALS['deps'][$n . '@' . $pn]) ? $n . '@' . $pn : $pn); } $v = $v instanceof Closure ? $v(...$a) : new $v(...$a); return $v; } // Usage: class A { function __construct($a, $b) { echo $a + $b; // 3 } function b() { echo 4; } } class B { function __construct(A $a) { $a->b(); } } class C { function __construct(B $a) { } } dep_set(A::class, function () { return new A(1, 2); }); $c = dep_get(C::class); // Instantiates A, B, C and prints "34"
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
function name:  (null)
number of ops:  10
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   INIT_FCALL                                               'dep_set'
          1        SEND_VAL                                                 'A'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Ftbe6i%3A46%240'
   48     3        SEND_VAL                                                 ~1
          4        DO_FCALL                                      0          
   50     5        INIT_FCALL                                               'dep_get'
          6        SEND_VAL                                                 'C'
          7        DO_FCALL                                      0  $3      
          8        ASSIGN                                                   !0, $3
          9      > RETURN                                                   1

Function dep_set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
function name:  dep_set
number of ops:  7
compiled vars:  !0 = $n, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        FETCH_W                      global              $2      'GLOBALS'
          3        FETCH_DIM_W                                      $3      $2, 'deps'
          4        ASSIGN_DIM                                               $3, !0
          5        OP_DATA                                                  !1
    6     6      > RETURN                                                   null

End of function dep_set

Function dep_get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 59
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 59
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 40
Branch analysis from position: 33
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 53
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 68
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 59
Branch analysis from position: 26
Branch analysis from position: 59
Branch analysis from position: 8
filename:       /in/tbe6i
function name:  dep_get
number of ops:  77
compiled vars:  !0 = $n, !1 = $v, !2 = $r, !3 = $a, !4 = $p, !5 = $i, !6 = $pc, !7 = $pn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
    9     1        FETCH_W                      global              $8      'GLOBALS'
          2        FETCH_DIM_W                                      $9      $8, 'deps'
          3        FETCH_DIM_W                                      $10     $9, !0
          4        ASSIGN_REF                                               !1, $10
   10     5        BOOL_NOT                                         ~12     !1
          6      > JMPZ                                                     ~12, ->8
          7    >   ASSIGN                                                   !1, !0
   11     8    >   INSTANCEOF                                               !1, 'Closure'
          9      > JMPZ                                                     ~14, ->15
         10    >   NEW                                              $15     'ReflectionFunction'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
         13        QM_ASSIGN                                        ~17     $15
         14      > JMP                                                      ->21
         15    >   NEW                                              $18     'ReflectionClass'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0          
         18        INIT_METHOD_CALL                                         $18, 'getConstructor'
         19        DO_FCALL                                      0  $20     
         20        QM_ASSIGN                                        ~17     $20
         21    >   ASSIGN                                                   !2, ~17
   12    22        ASSIGN                                                   !3, <array>
   13    23        INIT_METHOD_CALL                                         !2, 'getParameters'
         24        DO_FCALL                                      0  $23     
         25      > FE_RESET_R                                       $24     $23, ->59
         26    > > FE_FETCH_R                                       ~25     $24, !4, ->59
         27    >   ASSIGN                                                   !5, ~25
   14    28        INIT_METHOD_CALL                                         !4, 'getClass'
         29        DO_FCALL                                      0  $27     
         30        ASSIGN                                                   !6, $27
   15    31        BOOL_NOT                                         ~29     !6
         32      > JMPZ                                                     ~29, ->40
         33    >   NEW                                              $30     'Exception'
         34        FETCH_OBJ_R                                      ~31     !4, 'name'
         35        CONCAT                                           ~32     'Cannot+magically+resolve+a+param+%24', ~31
         36        CONCAT                                           ~33     ~32, '+without+class+typehint'
         37        SEND_VAL_EX                                              ~33
         38        DO_FCALL                                      0          
         39      > THROW                                         0          $30
   16    40    >   FETCH_OBJ_R                                      ~35     !6, 'name'
         41        ASSIGN                                                   !7, ~35
   17    42        INIT_FCALL_BY_NAME                                       'dep_get'
         43        CONCAT                                           ~40     !0, '%40'
         44        CONCAT                                           ~41     ~40, !7
         45        FETCH_IS                                         ~38     'GLOBALS'
         46        FETCH_DIM_IS                                     ~39     ~38, 'deps'
         47        ISSET_ISEMPTY_DIM_OBJ                         0          ~39, ~41
         48      > JMPZ                                                     ~42, ->53
         49    >   CONCAT                                           ~43     !0, '%40'
         50        CONCAT                                           ~44     ~43, !7
         51        QM_ASSIGN                                        ~45     ~44
         52      > JMP                                                      ->54
         53    >   QM_ASSIGN                                        ~45     !7
         54    >   SEND_VAL_EX                                              ~45
         55        DO_FCALL                                      0  $46     
         56        ASSIGN_DIM                                               !3
         57        OP_DATA                                                  $46
   13    58      > JMP                                                      ->26
         59    >   FE_FREE                                                  $24
   19    60        INSTANCEOF                                               !1, 'Closure'
         61      > JMPZ                                                     ~47, ->68
         62    >   INIT_DYNAMIC_CALL                                        !1
         63        SEND_UNPACK                                              !3
         64        CHECK_UNDEF_ARGS                                         
         65        DO_FCALL                                      1  $48     
         66        QM_ASSIGN                                        ~49     $48
         67      > JMP                                                      ->74
         68    >   FETCH_CLASS                                   0  $50     !1
         69        NEW                                              $51     $50
         70        SEND_UNPACK                                              !3
         71        CHECK_UNDEF_ARGS                                         
         72        DO_FCALL                                      1          
         73        QM_ASSIGN                                        ~49     $51
         74    >   ASSIGN                                                   !1, ~49
   20    75      > RETURN                                                   !1
   21    76*     > RETURN                                                   null

End of function dep_get

Function %00%7Bclosure%7D%2Fin%2Ftbe6i%3A46%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
function name:  {closure}
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $0      'A'
          1        SEND_VAL_EX                                              1
          2        SEND_VAL_EX                                              2
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $0
   48     5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Ftbe6i%3A46%240

Class A:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
function name:  __construct
number of ops:  5
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   27     2        ADD                                              ~2      !0, !1
          3        ECHO                                                     ~2
   28     4      > RETURN                                                   null

End of function __construct

Function b:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
function name:  b
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   ECHO                                                     4
   32     1      > RETURN                                                   null

End of function b

End of class A.

Class B:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
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        INIT_METHOD_CALL                                         !0, 'b'
          2        DO_FCALL                                      0          
   38     3      > RETURN                                                   null

End of function __construct

End of class B.

Class C:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tbe6i
function name:  __construct
number of ops:  2
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   43     1      > RETURN                                                   null

End of function __construct

End of class C.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.48 ms | 1415 KiB | 15 Q