<code>interface Pay { public function run(); } class Weixin implements Pay { public function run() { echo 'weixin'; } } class Ali implements Pay { public function run() { echo 'ali'; } } class PayServiceProvider extends ServiceProvider { public function register() { $this->app->singleton('weixin', function () { return new Weixin; }); $this->app->singleton('ali', function () { return new Ali; }); } } class A { // 这里注入应该填入什么参数? 怎么确定注入的是哪个类的实例 public functions __construct(Pay $pay) { } } </code>
<code>interface Pay { public function run(); } class Weixin implements Pay { public function run() { echo 'weixin'; } } class Ali implements Pay { public function run() { echo 'ali'; } } class PayServiceProvider extends ServiceProvider { public function register() { $this->app->singleton('weixin', function () { return new Weixin; }); $this->app->singleton('ali', function () { return new Ali; }); } } class A { // 这里注入应该填入什么参数? 怎么确定注入的是哪个类的实例 public functions __construct(Pay $pay) { } } </code>
Service Container
)! First, services defined by binding service ID
can only be obtained through service ID
, as follows:
<code>// 定义服务 $this->app->singleton('service_id', function () { return new Service(); }) // 获取服务,以下方式等价 $this->app->make('service_id'); $this->app['service_id'];</code>
Second, if you want to automatically parse and inject services through the interface’s type hint (Type Hint
), you need to do something like the following:
<code>// 绑定接口到实例 $this->app->bind('Namespace\To\Your\Interface\Pay', 'Namespace\To\Your\Class\Weixin');</code>
If the object you need to inject into the service is parsed through the service container (Service Container
), you can use type hints (Type Hint
) to automatically parse and inject the service. Instances of the following classes are resolved through the service container: controllers, event listeners, queue jobs, middleware
, etc., as well as services bound to the service container (Service Container
).
Third, like the above, by binding the interface to the instance, it can only be automatically resolved to one instance, which is the instance you bind; if the result you want is a different class, the constructor uses type hints ( Type Hint
) The same interface can be injected into different instances as follows (Context Binding, Context Binding
):
<code>$this->app->when('Namespace\To\Your\Class\A') ->needs('Namespace\To\Your\Interface\Pay') ->give('Namespace\To\Your\Class\Weixin'); $this->app->when('Namespace\To\Your\Class\B') ->needs('Namespace\To\Your\Interface\Pay') ->give('Namespace\To\Your\Class\Ali');</code>
Through context binding, the instance of A will be injected into the instance of Weixin, and the instance of B will be injected into the instance of Ali. Like below:
<code>class A { public functions __construct(Pay $pay) { var_dump($pay instanceof Weixin); // True } } class B { public functions __construct(Pay $pay) { var_dump($pay instanceof Ali); // True } }</code>