3v4l.org

run code in 300+ PHP versions simultaneously
<?php # basic container class Container { private $raw; // callbacks private $values; // objects private $locked; public function __construct(array $config = []) { foreach($config as $id => $callback){ $this->set($id, $callback); } } public function set(string $id, callable $callback): void { if(isset($this->locked[$id])){ throw new LockedIdException(); } $this->raw[$id] = $callback; } public function get(string $id) { if(!isset($this->raw[$id])){ throw new UnknownIdException(); } if(isset($this->values[$id])){ return $this->values[$id]; } $cb = $this->raw[$id]; $val = $this->values[$id] = $cb($this); $this->locked[$id] = true; return $val; } public function has(string $id): bool { return isset($this->values[$id]); } } class Resolver { public function __construct(Container $container, WireRule ...$rules) { $this->c = $container; $this->rules = $rules; } public function get(string $class) { if(!$this->c->has($class)){ $this->c->set($class, $this->resolve($class)); } return $this->c->get($class); } public function resolve(string $class): callable { if(!class_exists($class)){ throw new UnknownIdException(); } if(!method_exists($class, '__construct')){ return function(Container $c) use ($class){ return new $class; }; } $constructor = new \ReflectionMethod($class, '__construct'); if(count($params = $constructor->getParameters()) === 0){ return function(Container $c) use ($class){ return new $class; }; } foreach($params as $param){ $args[] = $param->hasType() ? $this->getArgument($param->getType()->__toString()) : null; } return function(Container $c) use ($class, $args){ return new $class(...$args); }; } public function getArgument(string $type) { foreach($this->rules as $rule){ if($rule->match($type)){ $type = $rule->getWiredClass($type); } } return $type ? $this->get($type) : null; } } class Foo { public function __construct(Bar $bar){ $this->bar = $bar; }} class Bar { public function __construct(Baz $baz){ $this->baz = $baz; }} class Joe { public $bob = 'bob'; } class Baz {} $c = new Resolver(new Container); var_dump($c->get(Foo::class)); var_dump($c->get(Baz::class)); var_dump($c->get(Joe::class));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  (null)
number of ops:  25
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   NEW                                              $1      'Resolver'
          1        NEW                                              $2      'Container'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $2
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $1
  111     6        INIT_FCALL                                               'var_dump'
          7        INIT_METHOD_CALL                                         !0, 'get'
          8        SEND_VAL_EX                                              'Foo'
          9        DO_FCALL                                      0  $6      
         10        SEND_VAR                                                 $6
         11        DO_ICALL                                                 
  112    12        INIT_FCALL                                               'var_dump'
         13        INIT_METHOD_CALL                                         !0, 'get'
         14        SEND_VAL_EX                                              'Baz'
         15        DO_FCALL                                      0  $8      
         16        SEND_VAR                                                 $8
         17        DO_ICALL                                                 
  113    18        INIT_FCALL                                               'var_dump'
         19        INIT_METHOD_CALL                                         !0, 'get'
         20        SEND_VAL_EX                                              'Joe'
         21        DO_FCALL                                      0  $10     
         22        SEND_VAR                                                 $10
         23        DO_ICALL                                                 
         24      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FM01FL%3A71%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c, !1 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_CLASS                                   0  $2      !1
          3        NEW                                              $3      $2
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $3
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FM01FL%3A71%240

Function %00%7Bclosure%7D%2Fin%2FM01FL%3A77%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c, !1 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_CLASS                                   0  $2      !1
          3        NEW                                              $3      $2
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $3
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FM01FL%3A77%241

Function %00%7Bclosure%7D%2Fin%2FM01FL%3A84%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $c, !1 = $class, !2 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   85     3        FETCH_CLASS                                   0  $3      !1
          4        NEW                                              $4      $3
          5        SEND_UNPACK                                              !2
          6        CHECK_UNDEF_ARGS                                         
          7        DO_FCALL                                      1          
          8      > RETURN                                                   $4
   86     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FM01FL%3A84%242

Class Container:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 9
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/M01FL
function name:  __construct
number of ops:  11
compiled vars:  !0 = $config, !1 = $callback, !2 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV_INIT                                        !0      <array>
   12     1      > FE_RESET_R                                       $3      !0, ->9
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->9
          3    >   ASSIGN                                                   !2, ~4
   13     4        INIT_METHOD_CALL                                         'set'
          5        SEND_VAR_EX                                              !2
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0          
   12     8      > JMP                                                      ->2
          9    >   FE_FREE                                                  $3
   15    10      > RETURN                                                   null

End of function __construct

Function set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  set
number of ops:  12
compiled vars:  !0 = $id, !1 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   19     2        FETCH_OBJ_IS                                     ~2      'locked'
          3        ISSET_ISEMPTY_DIM_OBJ                         0          ~2, !0
          4      > JMPZ                                                     ~3, ->8
   20     5    >   NEW                                              $4      'LockedIdException'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $4
   22     8    >   FETCH_OBJ_W                                      $6      'raw'
          9        ASSIGN_DIM                                               $6, !0
         10        OP_DATA                                                  !1
   23    11      > RETURN                                                   null

End of function set

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  get
number of ops:  30
compiled vars:  !0 = $id, !1 = $cb, !2 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        FETCH_OBJ_IS                                     ~3      'raw'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~4      ~3, !0
          3        BOOL_NOT                                         ~5      ~4
          4      > JMPZ                                                     ~5, ->8
   28     5    >   NEW                                              $6      'UnknownIdException'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $6
   31     8    >   FETCH_OBJ_IS                                     ~8      'values'
          9        ISSET_ISEMPTY_DIM_OBJ                         0          ~8, !0
         10      > JMPZ                                                     ~9, ->14
   32    11    >   FETCH_OBJ_R                                      ~10     'values'
         12        FETCH_DIM_R                                      ~11     ~10, !0
         13      > RETURN                                                   ~11
   35    14    >   FETCH_OBJ_R                                      ~12     'raw'
         15        FETCH_DIM_R                                      ~13     ~12, !0
         16        ASSIGN                                                   !1, ~13
   36    17        INIT_DYNAMIC_CALL                                        !1
         18        FETCH_THIS                                       $17     
         19        SEND_VAR_EX                                              $17
         20        DO_FCALL                                      0  $18     
         21        FETCH_OBJ_W                                      $15     'values'
         22        ASSIGN_DIM                                       ~16     $15, !0
         23        OP_DATA                                                  $18
         24        ASSIGN                                                   !2, ~16
   37    25        FETCH_OBJ_W                                      $20     'locked'
         26        ASSIGN_DIM                                               $20, !0
         27        OP_DATA                                                  <true>
   39    28      > RETURN                                                   !2
   40    29*     > RETURN                                                   null

End of function get

Function has:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  has
number of ops:  7
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   44     1        FETCH_OBJ_IS                                     ~1      'values'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   45     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function has

End of class Container.

Class Resolver:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  __construct
number of ops:  7
compiled vars:  !0 = $container, !1 = $rules
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   52     2        ASSIGN_OBJ                                               'c'
          3        OP_DATA                                                  !0
   53     4        ASSIGN_OBJ                                               'rules'
          5        OP_DATA                                                  !1
   54     6      > RETURN                                                   null

End of function __construct

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/M01FL
function name:  get
number of ops:  21
compiled vars:  !0 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
   58     1        FETCH_OBJ_R                                      ~1      'c'
          2        INIT_METHOD_CALL                                         ~1, 'has'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $2      
          5        BOOL_NOT                                         ~3      $2
          6      > JMPZ                                                     ~3, ->15
   59     7    >   FETCH_OBJ_R                                      ~4      'c'
          8        INIT_METHOD_CALL                                         ~4, 'set'
          9        SEND_VAR_EX                                              !0
         10        INIT_METHOD_CALL                                         'resolve'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0  $5      
         13        SEND_VAR_NO_REF_EX                                       $5
         14        DO_FCALL                                      0          
   61    15    >   FETCH_OBJ_R                                      ~7      'c'
         16        INIT_METHOD_CALL                                         ~7, 'get'
         17        SEND_VAR_EX                                              !0
         18        DO_FCALL                                      0  $8      
         19      > RETURN                                                   $8
   62    20*     > RETURN                                                   null

End of function get

Function resolve:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 34
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 35, Position 2 = 52
Branch analysis from position: 35
2 jumps found. (Code = 78) Position 1 = 36, Position 2 = 52
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 48
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
filename:       /in/M01FL
function name:  resolve
number of ops:  60
compiled vars:  !0 = $class, !1 = $constructor, !2 = $params, !3 = $param, !4 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   66     1        INIT_FCALL                                               'class_exists'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $5      
          4        BOOL_NOT                                         ~6      $5
          5      > JMPZ                                                     ~6, ->9
   67     6    >   NEW                                              $7      'UnknownIdException'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $7
   70     9    >   INIT_FCALL                                               'method_exists'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 '__construct'
         12        DO_ICALL                                         $9      
         13        BOOL_NOT                                         ~10     $9
         14      > JMPZ                                                     ~10, ->19
   71    15    >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FM01FL%3A71%240'
         16        BIND_LEXICAL                                             ~11, !0
         17        VERIFY_RETURN_TYPE                                       ~11
         18      > RETURN                                                   ~11
   74    19    >   NEW                                              $12     'ReflectionMethod'
         20        SEND_VAR_EX                                              !0
         21        SEND_VAL_EX                                              '__construct'
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !1, $12
   76    24        INIT_METHOD_CALL                                         !1, 'getParameters'
         25        DO_FCALL                                      0  $15     
         26        ASSIGN                                           ~16     !2, $15
         27        COUNT                                            ~17     ~16
         28        IS_IDENTICAL                                             ~17, 0
         29      > JMPZ                                                     ~18, ->34
   77    30    >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FM01FL%3A77%241'
         31        BIND_LEXICAL                                             ~19, !0
         32        VERIFY_RETURN_TYPE                                       ~19
         33      > RETURN                                                   ~19
   80    34    > > FE_RESET_R                                       $20     !2, ->52
         35    > > FE_FETCH_R                                               $20, !3, ->52
   81    36    >   INIT_METHOD_CALL                                         !3, 'hasType'
         37        DO_FCALL                                      0  $22     
         38      > JMPZ                                                     $22, ->48
         39    >   INIT_METHOD_CALL                                         'getArgument'
         40        INIT_METHOD_CALL                                         !3, 'getType'
         41        DO_FCALL                                      0  $23     
         42        INIT_METHOD_CALL                                         $23, '__toString'
         43        DO_FCALL                                      0  $24     
         44        SEND_VAR_NO_REF_EX                                       $24
         45        DO_FCALL                                      0  $25     
         46        QM_ASSIGN                                        ~26     $25
         47      > JMP                                                      ->49
         48    >   QM_ASSIGN                                        ~26     null
         49    >   ASSIGN_DIM                                               !4
         50        OP_DATA                                                  ~26
   80    51      > JMP                                                      ->35
         52    >   FE_FREE                                                  $20
   84    53        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FM01FL%3A84%242'
         54        BIND_LEXICAL                                             ~27, !0
         55        BIND_LEXICAL                                             ~27, !4
   86    56        VERIFY_RETURN_TYPE                                       ~27
         57      > RETURN                                                   ~27
   87    58*       VERIFY_RETURN_TYPE                                       
         59*     > RETURN                                                   null

End of function resolve

Function getargument:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 20
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/M01FL
function name:  getArgument
number of ops:  23
compiled vars:  !0 = $type, !1 = $rule
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
   91     1        FETCH_OBJ_R                                      ~2      'rules'
          2      > FE_RESET_R                                       $3      ~2, ->13
          3    > > FE_FETCH_R                                               $3, !1, ->13
   92     4    >   INIT_METHOD_CALL                                         !1, 'match'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $4      
          7      > JMPZ                                                     $4, ->12
   93     8    >   INIT_METHOD_CALL                                         !1, 'getWiredClass'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $5      
         11        ASSIGN                                                   !0, $5
   91    12    > > JMP                                                      ->3
         13    >   FE_FREE                                                  $3
   97    14      > JMPZ                                                     !0, ->20
         15    >   INIT_METHOD_CALL                                         'get'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0  $7      
         18        QM_ASSIGN                                        ~8      $7
         19      > JMP                                                      ->21
         20    >   QM_ASSIGN                                        ~8      null
         21    > > RETURN                                                   ~8
   98    22*     > RETURN                                                   null

End of function getargument

End of class Resolver.

Class Foo:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  __construct
number of ops:  4
compiled vars:  !0 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'bar'
          2        OP_DATA                                                  !0
          3      > RETURN                                                   null

End of function __construct

End of class Foo.

Class Bar:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M01FL
function name:  __construct
number of ops:  4
compiled vars:  !0 = $baz
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'baz'
          2        OP_DATA                                                  !0
          3      > RETURN                                                   null

End of function __construct

End of class Bar.

Class Joe: [no user functions]
Class Baz: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.31 ms | 1420 KiB | 19 Q