Call at the beginning of the code use Closure; this is the internal class php Closure and then the $ closure variable is used inside the function; From the manual it is absolutely not clear how to work and apply such a variable.

UPD

This is how it uses in the laravel framework, but I don’t understand what they are trying to achieve with this?

public function extend($abstract, Closure $closure) { $abstract = $this->normalize($abstract); if (isset($this->instances[$abstract])) { $this->instances[$abstract] = $closure($this->instances[$abstract], $this); $this->rebound($abstract); } else { $this->extenders[$abstract][] = $closure; } } 

    3 answers 3

    Closure are details of the closure implementation, i.e. anonymous functions.

     public function Name (Closure $closure){} 

    The entry states that one argument must be passed to the method and this argument must be a closure. Those. in the simple case like this:

     $object->Name(function() { // какие-то действия }); 

    A function is usually passed to a method with one purpose — for this method to be called somewhere. And you can call a function using different methods, for example:

     $closure(); // вот так call_user_func($closure); // или так 

    Or transfer to some other function that takes the callback parameter. You can pass additional arguments to the function.

    I note that it is rather strange to require Closure if you are going to use the methods of the Closure class. In general, to indicate the need to pass a function to a method, you must specify callable , which allows you to use any of the transmission options for this pseudotype .

      The object of the Closure class is not intended to be instantiated using the new operator, since it has a private constructor. This is the "utility" class of PHP, which is used to create anonymous functions .

      Design

       public function Name (Closure $closure){} 

      tells us that this function takes an anonymous function as a parameter. Calling it in the code will look like this:

       $variable = Name(function(параметры){что-то делаем;}); 

        This is how it uses in the laravel framework, but I don’t understand what they are trying to achieve with this.

         public function extend($abstract, Closure $closure) { $abstract = $this->normalize($abstract); if (isset($this->instances[$abstract])) { $this->instances[$abstract] = $closure($this->instances[$abstract], $this); $this->rebound($abstract); } else { $this->extenders[$abstract][] = $closure; } }