Laravel 自动注入和在构造函数体中手动指定依赖有什么区别?

What's the difference between Laravel automatic injection and manually specifying the dependencies in the constructor body?

我在我的 Laravel 项目中使用存储库模式。这种模式在官方文档中并没有真正解释,except for this snippet:

You may type-hint a repository defined by your application in a controller's constructor. The repository will automatically be resolved and injected into the class.

根据文档,这是我的代码:

class CategoriesController extends Controller
{
    protected $repo;

    public function __construct(CategoriesRepository $repo)
    {
        $this->repo = $repo;
    }

我已经对 CategoriesRepository 进行了类型提示,因此服务容器会自动加载它。

但是,如果我直接创建 CategoriesController 的新实例 class(不使用服务容器),我必须指定我也需要 CategoriesRepository 的新实例,如下所示:

$example = new CategoriesController(new CategoriesRepository());

现在,假设我编写了以下代码。

class CategoriesController extends Controller
{
    protected $repo;

    public function __construct()
    {
        $this->repo = new CategoriesRepository();
    }

这样,我不必通过服务容器加载 class,也不必通过将 CategoriesRepository 的新实例作为参数传递来调用它,因为它是在构造函数内部自动创建的。

所以,我的问题是:这是不好的做法吗? 类型提示作为参数和在构造函数中创建新实例有什么区别?

这里是依赖注入的美妙之处:

复杂初始化

class MyController {

     public function __construct(A $a) { }
}

class A {
     public function __construct(B $b) { }
}
class B {
     public function __construct(C $c) { }
}

class C {
     public function __construct(D $d) { }
}
class D {
     public function __construct() { }
}

现在您可以要求 laravel 为您创建 class 例如:

$controller = make(MyController::class);

或者你可以这样做:

$controller = new MyController(new A(new B(new C(new D())))));

此外,您可以指定关于如何创建变量的更复杂的规则:

app()->bind(D::class, function ($app) {
       $d = new D();
       $d->setValueOfSomething($app->make(AnotherClass::class));
       return $d;
});

测试

这是依赖注入相对于手动创建事物的优势之一。另一个是单元测试:

    public function testSomeFunctionOfC() {
        $this->app->bind(D::class, function () {
               $dMock = $this->createMock(D::class);
        });
        $c = make(C::class);
    }

现在,当您创建 C 时,class D 将成为模拟的 class,您可以确保根据您的规范工作。