When it comes to closures, we have to think of anonymous functions, also called closure functions (closures), which seems to be the key to PHP closure implementations. Declaring an anonymous function is this:
$func = function () {
};//with Terminator
you can see that the anonymous function, because it has no name, needs to be returned to a variable if it is to be used. Anonymous functions can also declare parameters like normal functions, and invoke the same method:
$func = function ($param) {
echo $param;
};
$func (' some string ');
Output:
//some string
Incidentally, PHP has a function to create anonymous functions before it introduces closures: the CREATE function, but the code logic can only be written as a string, so it looks obscure and difficult to maintain, so it's rarely used.
1, to achieve closure
An anonymous function is passed as a parameter in a normal function, or it can be returned. This implements a simple closure.
There are three examples below
Example one
//defines an anonymous function in a function, and calls it function
Printstr () {
$func = function ($str) {
echo $str;
};
$func (' some string ');
}
Printstr ();
Example II
//Returns the anonymous function in the function and calls it function
Getprintstrfunc () {
$func = function ($str) {
echo $str;
}; return
$func;
}
$printStrFunc = Getprintstrfunc ();
$printStrFunc (' some string ');
Example III
//pass the anonymous function as a parameter and call it function
Callfunc ($func) {
$func (' some string ');
}
$printStrFunc = function ($str) {
echo $str;
};
Callfunc ($printStrFunc);
Anonymous functions can also be passed directly. If you know JS, this type of writing may be familiar to
callfunc (function ($str) {
echo $str;
});
2, the connection closure and external variables keywords: use
Closures can hold variables and values in the context of the code block in which they reside. PHP by default, anonymous functions cannot invoke the context variables of the code block in which they reside, but rather by using the USE keyword.
Take another example to see:
function Getmoney () {
$RMB = 1;
$dollar = 6;
$func = function () use ($RMB) {
echo $rmb;
echo $dollar;
};
$func ();
}
Getmoney ();
Output:
//1
//error, dorllar variable not found
As you can see, dollar is not declared in the Use keyword and cannot be acquired in this anonymous function, so pay attention to this problem in development.
One might think that it is possible to change the context variable in an anonymous function, but I find that it is not possible:
function Getmoney () {
$RMB = 1;
$func = function () use ($RMB) {
echo $rmb;
Add the value of the $RMB to 1
$RMB + +;
$func ();
echo $rmb;
}
Getmoney ();
Output:
//1
//1
Ah, the original use of the reference is only a copy of the variable. But I want to make a full reference to the variable instead of copying it.
To achieve this effect, in fact, add a & symbol before the variable can be:
function Getmoney () {
$RMB = 1;
$func = function () use (& $RMB) {
echo $rmb;
Add the value of the $RMB to 1
$RMB + +;
$func ();
echo $rmb;
}
Getmoney ();
Output:
//1
//2
OK, so the anonymous function can refer to the variables of the context. If the anonymous function is returned to the outside world, the anonymous function saves the variables referenced by the use and the outside cannot get them, so the concept of ' closure ' may be clearer.
Change the above example according to the description:
function Getmoneyfunc () {
$RMB = 1;
$func = function () use (& $RMB) {
echo $rmb;
Add the value of the $RMB to 1
$RMB + +;
return $func;
}
$getMoney = Getmoneyfunc ();
$getMoney ();
$getMoney ();
$getMoney ();
Output:
//1
//2
//3
Summarize
The characteristics of the PHP closure is not too big surprise, in fact, with class can achieve similar or even more powerful functions, and JS can not be compared to the closure, can only look forward to PHP later on the closure support improvements. However, anonymous functions are useful, such as using functions such as preg_replace_callback to declare callback functions outside of the external declaration.
Small partners whether the anonymous function of PHP is the closure function has a new understanding of it, I hope this article can give you some tips, I hope you can enjoy.