. , - .
?
, . , , . , factory .
. , Container. (.. newModel), , , .
"" factory, , , . , : GenericService . GenericService. , add() getInstance(). -, , !
?
. . , . , , . : SOLID? , .
?
, : , , , . DIC , , .. .
Container, , . , . ( ).
?
. 1 3 : , . PHP-DI , . : autowiring , , - . autowiring, .
?
, . , (, , ..). (: , ).
:
<?php
class Injector
{
public function make($className)
{
$dependencies = [];
$classReflection = new ReflectionMethod($className, "__construct");
foreach($classReflection->getParameters() as $parameter) {
$dependencyName = $parameter->getClass()->getName();
$dependencies[] = $this->make($dependencyName);
}
$class = new ReflectionClass($className);
return $class->newInstanceArgs($dependencies);
}
}
- , ,
class A {
protected $b;
public function __construct(B $b) { $this->b = $b; }
public function output(){ $this->b->foo(); }
}
class B {
protected $c;
public function __construct(C $c) { $this->c = $c; }
public function foo() { $this->c->bar(); }
}
class C {
public function __construct() { }
public function bar() { echo "World!"; }
}
$injector = new Injector;
$a = $injector->make("A");
$a->output();
. , , ( ). , , , .
" ", make(). , , share(). , . , make() , .
Auryn, .