Dependency injection
Dependency injection is a fancy term, but it actually means: the dependencies of a class are "injected" through the constructor or in some cases through the "setter" method. Let’s first look at a code example in a Laravel controller:
<?php namespace App\Http\Controllers; use Illuminate\Routing\Controller; use App\Users\Repository as UserRepository; class UserController extends Controller { /** * 用户 Repository 的实例。 */ protected $users; /** * 创建一个新的控制器实例。 * * @param UserRepository $users * @return void */ public function __construct(UserRepository $users) { $this->users = $users; } /** * 显示指定 ID 的用户。 * * @param int $id * @return View */ public function show($id) { $user_info = $this->users->find($id); return view('user', ['user_info' => $user_info]); } }
Laravel manages class dependencies and performs dependency injection through service containers. If you use an interface as a type hint for function parameters, you need to bind the specified implementation to the interface:
interface EventPusher { public function send($data); }
class RedisEventPusher implements EventPusher { public function send($data) { // } }
$this->app->bind('App\Contracts\EventPusher', 'App\Services\RedisEventPusher');
use App\Contracts\EventPusher; /** * 创建一个新的类实例。 * * @param EventPusher $pusher * @return void */ public function __construct(EventPusher $pusher) { $this->pusher = $pusher; }
This is the so-called interface-oriented programming. The interface can be understood as a specification and a constraint. High-level modules do not directly depend on low-level modules, they should all depend on abstractions (referred to as interfaces).
The most important benefit of using dependency injection is that it effectively separates the object and the external resources it requires, making them loosely coupled, which is conducive to function reuse. More importantly, it makes the entire architecture of the program very flexible. .
Inversion of Control
Inversion of Control (Inversion of Control, abbreviated as IoC) is a design principle in object-oriented programming. The most common method is called Dependency Injection (DI), and the other method is called "Dependency Lookup". Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it. It can also be said that dependencies are injected into the object.
<?php /** * 没有IoC/DI的时候,常规的A类使用C类的示例 */ /** * Class c */ class c { public function say() { echo 'hello'; } } /** * Class a */ class a { private $c; public function __construct() { $this->c = new C(); // 实例化创建C类 } public function sayC() { echo $this->c->say(); // 调用C类中的方法 } } $a = new a(); $a->sayC();
When there is an IoC/DI container, class A no longer actively creates C, as shown in the figure below:
Instead, it passively waits for the IoC/DI container to obtain a C instance, and then reversely inject it into class A, as shown in the figure below:
<?php /** * 当有了IoC/DI的容器后,a类依赖c实例注入的示例 */ /** * Class c */ class c { public function say() { echo 'hello'; } } /** * Class a */ class a { private $c; public function setC(C $c) { $this->c = $c; // 实例化创建C类 } public function sayC() { echo $this->c->say(); // 调用C类中的方法 } } $c = new C(); $a = new a(); $a->setC($c); $a->sayC();