3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Container { private array $factories = []; public function addFactory(string $className, Factory $factory): void { $this->factories[$className] = $factory; } public function getInstance(string $className): object { return $this->factories[$className]->create(); } } abstract class Factory { private Container $container; private array $config = []; public function __construct(Container $container, array $config = []) { $this->container = $container; $instanceConfig = array_key_exists(static::getClass(), $config) ? $config[static::getClass()] : []; $this->config = $instanceConfig; } abstract public static function getClass(): string; public function create(): object { $class = static::getClass(); $parameters = []; foreach($this->config['parameters'] as $name => $value) { if (null === $value) { $value = $this->container->getInstance($name); } $parameters[] = $value; } return new $class(...$parameters); } } class FooFactory extends Factory { public static function getClass(): string { return Foo::class; } } class BarFactory extends Factory { public static function getClass(): string { return Bar::class; } } class Foo { private Bar $bar; private string $test; public function __construct(Bar $bar, string $test) { $this->bar = $bar; $this->test = $test; } } class Bar {} $config = [ Foo::class => [ 'parameters' => [ Bar::class => null, 'test', ] ], Bar::class => [ 'parameters' => [], ], ]; $container = new Container; $container->addFactory(FooFactory::getClass(), new FooFactory($container, $config)); $container->addFactory(BarFactory::getClass(), new BarFactory($container, $config)); $foo = $container->getInstance(Foo::class); var_dump($container); var_dump($foo);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  (null)
number of ops:  35
compiled vars:  !0 = $config, !1 = $container, !2 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   ASSIGN                                                   !0, <array>
   86     1        NEW                                              $4      'Container'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
   87     4        INIT_METHOD_CALL                                         !1, 'addFactory'
          5        INIT_STATIC_METHOD_CALL                                  'FooFactory', 'getClass'
          6        DO_FCALL                                      0  $7      
          7        SEND_VAR_NO_REF_EX                                       $7
          8        NEW                                              $8      'FooFactory'
          9        SEND_VAR_EX                                              !1
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0          
         12        SEND_VAR_NO_REF_EX                                       $8
         13        DO_FCALL                                      0          
   88    14        INIT_METHOD_CALL                                         !1, 'addFactory'
         15        INIT_STATIC_METHOD_CALL                                  'BarFactory', 'getClass'
         16        DO_FCALL                                      0  $11     
         17        SEND_VAR_NO_REF_EX                                       $11
         18        NEW                                              $12     'BarFactory'
         19        SEND_VAR_EX                                              !1
         20        SEND_VAR_EX                                              !0
         21        DO_FCALL                                      0          
         22        SEND_VAR_NO_REF_EX                                       $12
         23        DO_FCALL                                      0          
   89    24        INIT_METHOD_CALL                                         !1, 'getInstance'
         25        SEND_VAL_EX                                              'Foo'
         26        DO_FCALL                                      0  $15     
         27        ASSIGN                                                   !2, $15
   91    28        INIT_FCALL                                               'var_dump'
         29        SEND_VAR                                                 !1
         30        DO_ICALL                                                 
   92    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !2
         33        DO_ICALL                                                 
         34      > RETURN                                                   1

Class Container:
Function addfactory:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  addFactory
number of ops:  6
compiled vars:  !0 = $className, !1 = $factory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        FETCH_OBJ_W                                      $2      'factories'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
    9     5      > RETURN                                                   null

End of function addfactory

Function getinstance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  getInstance
number of ops:  9
compiled vars:  !0 = $className
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   13     1        FETCH_OBJ_R                                      ~1      'factories'
          2        FETCH_DIM_R                                      ~2      ~1, !0
          3        INIT_METHOD_CALL                                         ~2, 'create'
          4        DO_FCALL                                      0  $3      
          5        VERIFY_RETURN_TYPE                                       $3
          6      > RETURN                                                   $3
   14     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function getinstance

End of class Container.

Class Factory:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  __construct
number of ops:  18
compiled vars:  !0 = $container, !1 = $config, !2 = $instanceConfig
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   23     2        ASSIGN_OBJ                                               'container'
          3        OP_DATA                                                  !0
   25     4        INIT_STATIC_METHOD_CALL                                  'getClass'
          5        DO_FCALL                                      0  $4      
          6        ARRAY_KEY_EXISTS                                         $4, !1
          7      > JMPZ                                                     ~5, ->13
          8    >   INIT_STATIC_METHOD_CALL                                  'getClass'
          9        DO_FCALL                                      0  $6      
         10        FETCH_DIM_R                                      ~7      !1, $6
         11        QM_ASSIGN                                        ~8      ~7
         12      > JMP                                                      ->14
         13    >   QM_ASSIGN                                        ~8      <array>
         14    >   ASSIGN                                                   !2, ~8
   26    15        ASSIGN_OBJ                                               'config'
         16        OP_DATA                                                  !2
   27    17      > RETURN                                                   null

End of function __construct

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

End of function getclass

Function create:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 19
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 19
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/MhPOH
function name:  create
number of ops:  29
compiled vars:  !0 = $class, !1 = $parameters, !2 = $value, !3 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   INIT_STATIC_METHOD_CALL                                  'getClass'
          1        DO_FCALL                                      0  $4      
          2        ASSIGN                                                   !0, $4
   34     3        ASSIGN                                                   !1, <array>
   35     4        FETCH_OBJ_R                                      ~7      'config'
          5        FETCH_DIM_R                                      ~8      ~7, 'parameters'
          6      > FE_RESET_R                                       $9      ~8, ->19
          7    > > FE_FETCH_R                                       ~10     $9, !2, ->19
          8    >   ASSIGN                                                   !3, ~10
   36     9        TYPE_CHECK                                    2          !2
         10      > JMPZ                                                     ~12, ->16
   37    11    >   FETCH_OBJ_R                                      ~13     'container'
         12        INIT_METHOD_CALL                                         ~13, 'getInstance'
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0  $14     
         15        ASSIGN                                                   !2, $14
   40    16    >   ASSIGN_DIM                                               !1
         17        OP_DATA                                                  !2
   35    18      > JMP                                                      ->7
         19    >   FE_FREE                                                  $9
   43    20        FETCH_CLASS                                   0  $17     !0
         21        NEW                                              $18     $17
         22        SEND_UNPACK                                              !1
         23        CHECK_UNDEF_ARGS                                         
         24        DO_FCALL                                      1          
         25        VERIFY_RETURN_TYPE                                       $18
         26      > RETURN                                                   $18
   44    27*       VERIFY_RETURN_TYPE                                       
         28*     > RETURN                                                   null

End of function create

End of class Factory.

Class FooFactory:
Function getclass:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  getClass
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   'Foo'
   51     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function getclass

Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  __construct
number of ops:  18
compiled vars:  !0 = $container, !1 = $config, !2 = $instanceConfig
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   23     2        ASSIGN_OBJ                                               'container'
          3        OP_DATA                                                  !0
   25     4        INIT_STATIC_METHOD_CALL                                  'getClass'
          5        DO_FCALL                                      0  $4      
          6        ARRAY_KEY_EXISTS                                         $4, !1
          7      > JMPZ                                                     ~5, ->13
          8    >   INIT_STATIC_METHOD_CALL                                  'getClass'
          9        DO_FCALL                                      0  $6      
         10        FETCH_DIM_R                                      ~7      !1, $6
         11        QM_ASSIGN                                        ~8      ~7
         12      > JMP                                                      ->14
         13    >   QM_ASSIGN                                        ~8      <array>
         14    >   ASSIGN                                                   !2, ~8
   26    15        ASSIGN_OBJ                                               'config'
         16        OP_DATA                                                  !2
   27    17      > RETURN                                                   null

End of function __construct

Function create:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 19
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 19
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/MhPOH
function name:  create
number of ops:  29
compiled vars:  !0 = $class, !1 = $parameters, !2 = $value, !3 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   INIT_STATIC_METHOD_CALL                                  'getClass'
          1        DO_FCALL                                      0  $4      
          2        ASSIGN                                                   !0, $4
   34     3        ASSIGN                                                   !1, <array>
   35     4        FETCH_OBJ_R                                      ~7      'config'
          5        FETCH_DIM_R                                      ~8      ~7, 'parameters'
          6      > FE_RESET_R                                       $9      ~8, ->19
          7    > > FE_FETCH_R                                       ~10     $9, !2, ->19
          8    >   ASSIGN                                                   !3, ~10
   36     9        TYPE_CHECK                                    2          !2
         10      > JMPZ                                                     ~12, ->16
   37    11    >   FETCH_OBJ_R                                      ~13     'container'
         12        INIT_METHOD_CALL                                         ~13, 'getInstance'
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0  $14     
         15        ASSIGN                                                   !2, $14
   40    16    >   ASSIGN_DIM                                               !1
         17        OP_DATA                                                  !2
   35    18      > JMP                                                      ->7
         19    >   FE_FREE                                                  $9
   43    20        FETCH_CLASS                                   0  $17     !0
         21        NEW                                              $18     $17
         22        SEND_UNPACK                                              !1
         23        CHECK_UNDEF_ARGS                                         
         24        DO_FCALL                                      1          
         25        VERIFY_RETURN_TYPE                                       $18
         26      > RETURN                                                   $18
   44    27*       VERIFY_RETURN_TYPE                                       
         28*     > RETURN                                                   null

End of function create

End of class FooFactory.

Class BarFactory:
Function getclass:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  getClass
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E > > RETURN                                                   'Bar'
   58     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function getclass

Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MhPOH
function name:  __construct
number of ops:  18
compiled vars:  !0 = $container, !1 = $config, !2 = $instanceConfig
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   23     2        ASSIGN_OBJ                                               'container'
          3        OP_DATA                                                  !0
   25     4        INIT_STATIC_METHOD_CALL                                  'getClass'
          5        DO_FCALL                                      0  $4      
          6        ARRAY_KEY_EXISTS                                         $4, !1
          7      > JMPZ                                                     ~5, ->13
          8    >   INIT_STATIC_METHOD_CALL                                  'getClass'
          9        DO_FCALL                                      0  $6      
         10        FETCH_DIM_R                                      ~7      !1, $6
         11        QM_ASSIGN                                        ~8      ~7
         12      > JMP                                                      ->14
         13    >   QM_ASSIGN                                        ~8      <array>
         14    >   ASSIGN                                                   !2, ~8
   26    15        ASSIGN_OBJ                                               'config'
         16        OP_DATA                                                  !2
   27    17      > RETURN                                                   null

End of function __construct

Function create:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 19
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 19
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/MhPOH
function name:  create
number of ops:  29
compiled vars:  !0 = $class, !1 = $parameters, !2 = $value, !3 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   INIT_STATIC_METHOD_CALL                                  'getClass'
          1        DO_FCALL                                      0  $4      
          2        ASSIGN                                                   !0, $4
   34     3        ASSIGN                                                   !1, <array>
   35     4        FETCH_OBJ_R                                      ~7      'config'
          5        FETCH_DIM_R                                      ~8      ~7, 'parameters'
          6      > FE_RESET_R                                       $9      ~8, ->19
          7    > > FE_FETCH_R                                       ~10     $9, !2, ->19
          8    >   ASSIGN                                                   !3, ~10
   36     9        TYPE_CHECK                                    2          !2
         10      > JMPZ                                                     ~12, ->16
   37    11    >   FETCH_OBJ_R                                      ~13     'container'
         12        INIT_METHOD_CALL                                         ~13, 'getInstance'
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0  $14     
         15        ASSIGN                                                   !2, $14
   40    16    >   ASSIGN_DIM                                               !1
         17        OP_DATA                                                  !2
   35    18      > JMP                                                      ->7
         19    >   FE_FREE                                                  $9
   43    20        FETCH_CLASS                                   0  $17     !0
         21        NEW                                              $18     $17
         22        SEND_UNPACK                                              !1
         23        CHECK_UNDEF_ARGS                                         
         24        DO_FCALL                                      1          
         25        VERIFY_RETURN_TYPE                                       $18
         26      > RETURN                                                   $18
   44    27*       VERIFY_RETURN_TYPE                                       
         28*     > RETURN                                                   null

End of function create

End of class BarFactory.

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

End of function __construct

End of class Foo.

Class Bar: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.11 ms | 1024 KiB | 14 Q