1. What is closureClosure personal understanding is an anonymous function that provides local variables within an external access function
2. Why do you use closures?The closure definition has explained that we want to access local variables within the function externally, and we need to use closures for this purpose.
3. Closure of the use of
(0) The use of the premisePHP version >= 5.3.0
(1) Use of anonymous functions
We first implement a JS-like addition operation
$add = function ($type = ' number ') {
if ($type = = ' number ') {return
function ($a, $b) {return
$a + $b;
}
}
if ($type = = ' string ') {return
function ($a, $b) {return
$a. $b
;
}
}} $typeadd = $add (' number ');
$typeadd (1,23);
$typeadd = $add (' string ');
$typeadd (1,23); 123
(2) Closure instantiation Object
All anonymous functions are instantiated objects of the closure closure
Closure provides two very useful methods bindTo and bind, where users bind other object states to anonymous functions, allowing anonymous functions to access methods and properties of other objects.
A. First look at the bindto example "Getting Started with chestnuts"
Class A
{
private $a 1 = 1;
Protected $a 2 = 2;
Public $a 3 = 3;
}
$closure 1 = function ($member) {return
$this-> $member;
};
$a = new A ();
$aa 1 = $closure 1->bindto ($a);
echo $aa 1 (' A1 '); Private members cannot access the
//echo $aa 1 (' A2 ');//protection members cannot access
echo $aa 1 (' A3 ');
$aa 2 = $closure 1->bindto ($a, ' a ');
$aa 3 = $closure 1->bindto ($a, $a);
echo $aa 2 (' A1 ');
echo $aa 3 (' A2 ');
It's intuitive to see that BindTo has two parameters,
The first parameter is the object (object) to bind to.
The second parameter is the bounded range (object or class name) of the bound object
BindTo ($a)//only access to the public methods and properties of a
BindTo ($a, ' a ')
BindTo ($a, $a)//access to all methods and properties of a
If the second argument is the parent of the first parameter, then all methods and properties that can access only the parent class are qualified
and subclass public methods and properties
"Complex Example"
Laravel Routing
Class App
{
private $routes = [];
Private $response = ';
Public Function Addroute ($path, $callback) {
$this->routes[$path] = $callback->bindto ($this, __class__);
Public
function Dispatch ($currentPath) {
foreach ($this->routes as $route => $callback) {
if ($route = = = $currentPath) {
$callback ();
}
}
echo $this->response;
}
}
$app = new app ();
$app->addroute (' A/b ', function () {
$this->response = ' is A/b ';
});
$app->addroute (' C/D ', function () {
$this->response = ' this is C/D ';
});
$app->dispatch (' A/b ');
B. Bind is a static version of BindTo
(1) Static member variable or method of class
Closure::bind (Closurefunc,null,classname);
(2) Ordinary member variables or methods of a class
Closure::bind (Closurefunc,new ClassName (), ClassName);
(3) The most commonly used use
The most commonly used retention state or the Access function local variable is using the USE keyword
function A ($name)
{
$i = 0;
return function () use ($name, & $i) {
$i + +;
echo $name. ': ';
echo $i;}
;}
$a 1 = A (' Zhangsan ');
$a 2 = A (' Lisi ');
$a 1 (); Zhangsan:1
$a 1 (); the state of//zhangsan:2 name and I is reserved
$a 2 (); the state of//lisi:1 name and I is reinitialized
The respective states do not affect each other, but the respective variable states are always retained, which is use