Containers and dependency injection
Container and dependency injection (container)
Dependency injection is implemented through the container, which can realize Automatic introduction of the referenced class, and automatic loading of the dependent objects of the class
Dependency injection refers to the automatic injection of class dependencies through the constructor:
<?php namespace app\index\controller; use app\index\model\User; class Index { protected $user; // 通过依赖注入方式,实现User实例的自动注入到当前对象中 public function __construct(User $user) { $this->user = $user; } public function hello() { return 'Hello,' . $this->user->name . '!'; } }
Object parameter support for dependency injection Multiple, and the order is irrelevant.
Binding
Dependency injected classes are uniformly managed by the container, and in most cases are automatically bound and instantiated. However, you can manually bind the class to the container at any time (usually in the register method of the service class), and multiple binding methods are supported.
Bind class identification
You can bind an identification (unique) to an existing class library for quick calling.
// 绑定类库标识 $this->app->bind('cache', 'think\Cache');
Or use the helper function
// 绑定类库标识 bind('cache', 'think\Cache');
The bound class identifier can be defined by yourself (as long as there is no conflict).
Binding closure
You can bind a closure to the container
bind('sayHello', function ($name) { return 'hello,' . $name; });
Binding instance
You can also directly bind an instance of a class
$cache = new think\Cache; // 绑定类实例 bind('cache', $cache);
Bind to the interface implementation
For dependency injection using interface classes, we need Tell the system which specific interface implementation class to use for injection. This use can bind a certain class to the interface
// 绑定think\LoggerInterface接口实现到think\Log bind('think\LoggerInterface','think\Log');
Use the interface as the type of dependency injection
<?php namespace app\index\controller; use think\LoggerInterface; class Index { public function hello(LoggerInterface $log) { $log->record('hello,world!'); } }
Batch binding Determined
In the actual application development process, manual binding is not required. We only need to define the provider.php file under the application directory (returning an array), and the system will automatically bind the class library to the container in batches. middle.
return [ 'route' => \think\Route::class, 'session' => \think\Session::class, 'url' => \think\Url::class, ];
The binding identifier is case-sensitive when called. The system has already built-in binding core common class libraries, so there is no need to bind them repeatedly.
The system’s built-in class libraries bound to the container include
System class library | Container binding identifier |
---|---|
think\App | app |
think\Cache | cache |
think\Config | config |
think\Cookie | cookie |
think\Console | console |
think\Db | db |
think\Debug | debug |
think \Env | env |
think\Event | event |
think\Http | http |
lang | |
log | |
middleware | |
request | |
response | |
filesystem | |
route | |
session | |
validate | |
view |