Home > Backend Development > PHP Tutorial > Laravel Service Providers binds multiple implementations. How to determine which class is instantiated during dependency injection?

Laravel Service Providers binds multiple implementations. How to determine which class is instantiated during dependency injection?

WBOY
Release: 2016-08-04 09:21:50
Original
2181 people have browsed it

<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>
Copy after login
Copy after login

Reply content:

<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>
Copy after login
Copy after login

First of all, you have a wrong understanding of the behavior of the service container (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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template