注入类上的构造函数未被称为 Laravel IOC



我在测试类中注入了发票服务。调用发票服务的创建方法的测试类。 由于此$repo为空而未调用注入类的构造函数创建失败。 任何帮助,不胜感激。

$this->app->singleton(
'AppModelsSubscriptionInterfacesIInvoiceService',
'AppModelsSubscriptionImplInvoiceService'
);

class InvoiceService implements  IInvoiceService
{
protected $repo;

public function _construct(){
$this->app = App::getFacadeRoot();
$this->repo = $this->app['repo'];

}
public function Create($array)
{
$this->repo->insert($array); // //$this->repo is null as Constructor not getting called
}
}

class TestClass 
{
protected $InvoiceService;
public function _construct(IInvoiceService $InvoiceService){
$this->InvoiceService =$InvoiceService
}
public create($input) {
this->InvoiceService->create($input);  // This is failing
}
}

要覆盖默认功能,请使用__"两个下划线",在您的示例中,您只使用一个。

_construct()

应该是。

__construct()

最新更新