Laravel setter injection Laravel setter injection laravel laravel

Laravel setter injection


There is no way for you to ensure that other method is ran before sendEmail is ran and the only method that could have 'method injection' would be setMailer.

If you wanted method injection for the setMailer method you would have to use the IoC container to call that method:

$a = new Test;app()->call([$a, 'setMailer']);

This will have the container call setMailer on $a for you and will resolve any dependencies needed, in this case.

Having the container call sendEmail would be exactly the same as calling it yourself, as there are no arguments.

If you really wanted a mailer to be available, you could use constructor injection so you have the mailer before sendEmail is called.

public function __construct(Mailer $mailer){    $this->mailer = $mailer;}