cloud_download Stáhnout

Dependency injection

Použití:

class A
{
    public function hello(string $world): string
    {
        return "hello " . $world;
    }
}

class B
{
    private $a;

    public function __construct(A $a)
    {
        $this->a = $a;
    }

    public function render()
    {
        return $this->a->hello("world");
    }
}

// Bad
$a = new A();
$b = new B($a);
$b->render();

// Good
$container = new Container();
$b = $container->get(B::class);
$b->render(); // hello world