Loading…

Explain Closures in laravel?

In Laravel, A Closure is an anonymous method and used as callback functions and also can be used as a parameter in a process. It is possible to pass parameters into a Closure. We can do by changing the Closure function call in the handle method to give on a parameter.

Example

                                        

function handle(Closure $closure) {
    $closure();
}

handle(function(){
    echo ‘Best Interview Question’;
});

We can start by adding a Closure parameter to the handle method. This will be used as type hint us that the handle method takes a Closure.

We can call the handle method and pass a service as a parameter.

By using $closure(); in the handle method we tell Laravel to execute the given Closure which will then display ‘Best Interview Question.’

Suggest An Answer

No suggestions avaliable!

Latest post from Laravel Interview Questions

Ask Question