Laravel IOC没有调用构造函数 [英] Laravel IOC not calling the constructor
本文介绍了Laravel IOC没有调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务提供商中具有约束力
I have binding in the service provider
$this->app->singleton(
'App\Models\Subscription\Interfaces\IInvoiceService',
'App\Models\Subscription\Impl\InvoiceService'
);
class InvoiceService implements IInvoiceService
{
protected $repo;
public function _construct(){
$this->app = App::getFacadeRoot();
$this->repo = $this->app['repo'];
}
public function Create()
{
}
}
在注入了IInovoice服务"中的一个类中.
In one of the classes in Injected the IInovoice Service.
我正在获取IInovoice的具体实现.但是InvoiceService的构造函数永远不会被调用
I am getting the concrete implementation of IInovoice . but the Constructor of the InvoiceService is never getting called
推荐答案
尝试以下操作
class InvoiceService implements IInvoiceService
{
protected $app;
protected $repo;
public function _construct($app, $repo) {
$this->app = $app;
$this->repo = $repo;
}
public function Create() {
}
}
App \ Providers \ AppServiceProvider
App\Providers\AppServiceProvider
public function register() {
$this->app->singleton(
'App\Models\Subscription\Interfaces\IInvoiceService',
'App\Models\Subscription\Impl\InvoiceService'
);
$this->app->when('App\Models\Subscription\Impl\InvoiceService')
->needs('$app')
->give($this->app->getFacadeRoot());
$this->app->when('App\Models\Subscription\Impl\InvoiceService')
->needs('$repo')
->give($this->app['repo']);
}
public function boot(ResponseFactory $response) {
//uncomment this line to debug InvoiceService Resolving event
//$this->app->resolving(App\Models\Subscription\Impl\InvoiceService::class, function ($invoiceService, $app) {
//dump('resolves InvoiceService', $invoiceService);
//});
}
然后运行
php artisan clear-compiled
php artisan config:clear
这篇关于Laravel IOC没有调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文