Конструктор в классе Injected не вызывается Laravel IOC - PullRequest
0 голосов
/ 14 февраля 2020

Я внедряю InvoiceService в тестовый класс. Тестовый класс, вызывающий метод create службы invoice. Конструктор внедренного класса не вызывается из-за этого $ repo is null create не работает. Любая помощь приветствуется.

$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($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
    }
 }

1 Ответ

0 голосов
/ 14 февраля 2020

Для переопределения функциональности по умолчанию вы используете __ "два подчеркивания", в вашем примере вы используете только один.

_construct()

Должно быть.

__construct()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...