Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Closure in Laravel?

I saw one Laravel function in middlewere:

public function handle($request, Closure $next, $guard = null) {     if (Auth::guard($guard)->check())     {        return redirect('/home');     }       return $next($request); } 

What is Closure and what does it do?

like image 447
vishal ribdiya Avatar asked Nov 17 '17 10:11

vishal ribdiya


People also ask

What is a closure in PHP?

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

What is closure class?

The Closure class ¶Class used to represent anonymous functions. Anonymous functions yield objects of this type. This class has methods that allow further control of the anonymous function after it has been created. Besides the methods listed here, this class also has an __invoke method.

What is object closure?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

How do you write a closure?

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter's type to indicate that the closure is allowed to escape.


1 Answers

A Closure is an anonymous function. Closures are often used as callback methods and can be used as a parameter in a function.

If you take the following example:

function handle(Closure $closure) {     $closure(); }  handle(function(){     echo 'Hello!'; }); 

We start by adding a Closure parameter the handle function. This will type hint us that the handle function takes a Closure.

We then call the handle function and pass a function as the first parameter.

By using $closure(); in the handle function we tell PHP to execute the given Closure which will then echo 'Hello!'

It is also possible to pass parameters into a Closure. We can do so by changing the Closure call in the handle function to pass on a parameter. In this example i'll just pass a string but this can be any variable.

The handle function now looks like

function handle(Closure $closure) {     $closure('Hello World!'); } 

We now also need to modify the Closure itself to take the parameter. We do so by simply adding a parameter to the function. And then we pass that variable to the echo.

The function now looks like

handle(function($value){     echo $value; }); 

Which will echo Hello World!

For more information you can check out these links:

http://php.net/manual/en/functions.anonymous.php

http://php.net/manual/en/class.closure.php

like image 167
Amando Vledder Avatar answered Oct 10 '22 09:10

Amando Vledder