jeudi 13 mai 2021

Dependency injection, how to inject "other" object?

I always hear that it is hardcoded:

final class Dependency {}

class A
{
    private Dependency $dependency;

    public function __construct()
    {
        $this->dependency = new Dependency();
    }
}

ok, let's use dependency injection:

class A
{
    private Dependency $dependency;

    public function __construct(Dependency $dependency)
    {
        $this->dependency = $dependency;
    }
}

what does it make it better? I cant pass anything other than Dependency!

Aucun commentaire:

Enregistrer un commentaire