PHP closures and anonymous functions use the same syntax as normal functions, but closures and anonymous functions are actually objects disguised as functions (instances of the closure class). The following is a description of the closure and anonymous functions in PHP knowledge, the need for a friend to refer to the next closure is the function of encapsulating the surrounding state at the time of creation. The state of encapsulation in the closure is still present even if the environment in which the closure is located does not exist.
An anonymous function is a function that has no name. Anonymous functions can be assigned to variables and can be passed like any other PHP object. However, the anonymous function is still a function, so it can be called and passed in parameters. Anonymous functions are particularly well-suited as callbacks for functions or methods.
Note: In theory, closures and anonymous functions are different concepts. However, PHP sees it as the same concept. Therefore, when we refer to closures, we refer to anonymous functions and vice versa.
PHP closures and anonymous functions use the same syntax as normal functions, but closures and anonymous functions are actually objects disguised as functions (instances of the closure class).
Creating closures
?
123456 |
$closure = function ( $name    return sprintf ( "Hello%s" $name } echo $closure ( //detects if the $closure variable is a closure var_dump ( $closure instanceof closure); |
The above code creates a closure object and assigns it to the $closure variable. Closures are similar to normal PHP functions, using the same syntax, receiving parameters, and returning values.
Description: We were able to invoke the $closure variable because the value of the variable is a closure, and the closure object implements the __invoke () Magic method. As long as the variable name is followed by (), PHP will find and invoke the __invoke()
method.
Using closures
We usually use PHP closures as callbacks for functions and methods. Many PHP functions use callback functions, such as array_map()
and preg_replace_callback()
. The following example, we will use ARRAY_MAP () to process the array, each item of the array is increased by 1:
?
1234 |
$nubmers = array_map ( function ( $number ){ return $number ++; }, [1,2,3]); var_dump( $numbers ); |
Attach Status
PHP closures do not automatically encapsulate the state of the application like a real javascrypt closure, and we must manually invoke the BindTo () method of the closure object or use the Using keyword to attach the state to the PHP closure.
Using the USE keyword
Using the Use keyword to attach a closure state is more common, so let's look at this way first. When you attach a variable to a closed package by using the Use keyword, the attached variable remembers the value assigned to it when attached.
?
12345678910 |
function Car (
$name
){
return function
(
$statu
)
use
(
$name
){
return sprintf(
"Car %s is %s"
,
$name
,
$statu
);
}
}
// 将车名封装在闭包中
$car = Car(
"bmw"
);
// 调用车的动作
// 输出--> "bmw is running"
echo $car
(
"running"
);
|
Note: Using the USE keyword allows you to pass multiple parameters into a closure, which is to use commas to separate multiple parameters, just like the parameters of a PHP function or method.
Use the BindTo () method to attach the state of a closure
Similar to other PHP objects, each closure instance can use the $this keyword to get the internal state of the closure. The default state of a closure object is useless, but there is a __invoke () Magic Method and a BindTo () method.
The BindTo () method adds some interesting potential to closures. We can use this method to bind the internal state of the Closure object to other objects.
The second parameter of the BindTo () method is important to specify the PHP class to which the object that binds the closure belongs. Therefore, closures can access protected and private member variables in the object that binds the closure.
?
1234567891011121314151617181920212223242526272829303132 |
class TestClosure
{
private $name
=[];
private $age
;
private $sex
;
public function addPerson(
$name
,
$personCallback
){
// 将闭包对象绑定当前实例
$this
->name[
$name
] =
$personCallback
->bindTo(
$this
,
__CLASS__
);
}
public function display(
$name
){
foreach (
$this
->name
as $key =>
$callback
){
if
(
$key ==
$name
){
// 执行闭包对象,将闭包状态附加到类
$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
*/
|
A brief analysis of closures and anonymous functions in PHP