Analysis of Closure and Anonymous Function in PHP

  • 2021-08-31 07:32:23
  • OfStack

Closures are functions that encapsulate the surrounding state at creation time. Even if the environment in which the closure is located no longer exists, the state encapsulated in the closure still exists.

An anonymous function is a function without a name. Anonymous functions can be assigned to variables and can be passed like any other PHP object. However, anonymous functions are still functions, so they can be called and parameters can be passed in. Anonymous functions are especially suitable as callbacks to functions or methods.

Note: In theory, closures and anonymous functions are different concepts. However, PHP regards it as the same concept. Therefore, when we mention closures, we also mean anonymous functions, and vice versa.

PHP closures and anonymous functions use the same syntax as normal functions, but closures and anonymous functions are objects disguised as functions (instances of the Closure class).

Creating Closures


$closure = function($name){
  return sprintf("Hello %s", $name);
}
echo $closure("jerry");
//  Detection $closure Whether the variable is 1 Closure 
var_dump($closure instanceof Closure);

The above code creates a closure object and assigns it to the $closure variable. Closures are similar to ordinary PHP functions, using the same syntax, taking arguments, and returning values.

Explanation: We can call the $closure variable because the value of the variable is a closure and the closure object implements the __invoke () magic method. As long as there is () after the variable name, PHP will find and call __invoke() Method.

Using closures

We usually use PHP closures as callbacks to functions and methods. Many PHP functions use callback functions, such as array_map() And preg_replace_callback() In the following example, we will use array_map () to process the array, incrementing it by 1 for every item:


$nubmers = array_map(function($number){
  return $number++;
}, [1,2,3]);
var_dump($numbers);

Additional status

The PHP closure does not automatically encapsulate the state of the application like the real javascrypt closure, so we must manually call the bindTo () method of the closure object or use the use keyword to append the state to the PHP closure.

Use the use keyword

It is more common to use the use keyword to attach closure state, so let's look at this approach first. When you attach a variable to a closure using the use keyword, the attached variable remembers the value assigned to it when it was attached.


function Car ($name){
  return function($statu)use($name){
    return sprintf("Car %s is %s", $name, $statu); 
  }
}
//  Encapsulate the car name in a closure 
$car = Car("bmw");
//  Action of adjusting car 
//  Output --> "bmw is running"
echo $car("running");

Note: You can use the use keyword to pass multiple parameters into a closure, separating them with commas, as with parameter 1 of an PHP function or method.

The state of attaching closures using the bindTo () method

Like other PHP objects, each closure instance can use the $this keyword to get the internal state of the closure. The default state of the closure object is useless, but there is a __invoke () magic method and an bindTo () method.

The bindTo () method adds some interesting potential to closures. We can use this method to bind the internal state of an Closure object to other objects.

The second parameter of the bindTo () method is important to specify the PHP class to which the object to which the closure is bound belongs. Therefore, closures can access protected and private member variables in the object to which they are bound.


class TestClosure
{
  private $name=[];
  private $age;
  private $sex;
  public function addPerson($name, $personCallback){
    //  Bind the closure object to the current instance 
    $this->name[$name] = $personCallback->bindTo($this, __CLASS__);
  }
  public function display($name){
    foreach ($this->name as $key => $callback){
      if($key == $name){
        //  Executes the closure object, attaching the closure state to the class 
        $callback();
      }
    }
    echo "name : {$name}\n";
    echo "age : {$this->age}\n";
    echo "sex : {$this->sex}\n";
  }
}
$person = new TestClosure();
$person->addPerson("jerry", function(){
  $this->age = 19;
  $this->sex = "man";
});
$person->display("jerry");
/** output
name : jerry
age : 19
sex : man
*/

Summarize


Related articles: