Loading…

Explain Closures in laravel?

In Laravel, a closure is an anonymous function that often used as callback methods. Developers can also use it as a parameter in a function.

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