0

I read this post How to make function run in background in laravel, then I noticed this line.

$sendEmailJob = (new SendEmail($user))->onQueue('emails');

How did laravel call the ->onQueue method by wrapping new SendEmail($user) with parenthesis?

I ask this question because I don't know what exact term to search for that.

Fil
  • 8,225
  • 14
  • 59
  • 85

1 Answers1

1

That's a "fast way" to call a method inside a class, introduced in PHP 5.4.0 , assuming that you only need that method and nothing else from that class, you can do something like

(new myClass())->myMethod()

this will prevent to use unnecessary memory for a variable that you will never use again. Is the same if you do something like

$class = new myClass();
$class->myMethod();
unset($class);

reference: http://php.net/manual/en/language.oop5.basic.php#example-178

Gumma Mocciaro
  • 1,205
  • 10
  • 24