1,什么是依赖注入
我的理解是,为了降低系统耦合度,提高系统适用性,使得上层不依赖下层,实现自动绑定并实例化。
注入实例:
interface UserRepositoryInterface
{
public function all(): array;
}
class UserController extends BaseController
{
public function __construct(UserRepositoryInterface $users)
{
$this->users = $users;
}
public function getIndex()
{
$users=$this->users->all();
return View::make('users.index', compact('users'));
}
}
2,但是thinkphp5.1需要实现依赖注入的控制器不能继承Controller,因为初始化函数是他们自定义的
本文探讨了依赖注入的概念及其在降低系统耦合度和提高系统适用性方面的作用。通过具体实例展示了依赖注入在PHP框架中的应用,并特别提到了在ThinkPHP5.1中实现依赖注入的限制。
705

被折叠的 条评论
为什么被折叠?



