PHP Closure (Closure) Use of detailed _php examples

Source: Internet
Author: User
Tags closure

Unknowingly found that PHP has been out of the 5.5 version, and they have been using PHP5.2, let me look like the young man out of the mountains, and the soil behind. After I used to use closures in JavaScript, I suddenly became interested in the closure of the PHP package.

So on the Internet under a WAMP integrated development environment, is the PHP5.3 version (PHP5.3 began to introduce the characteristics of the closure), have to say Wamp installation is really convenient to use. Simple configuration for a moment, start.

anonymous functions
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:

Copy Code code as follows:

$func = function () {

}; With end character

As you can see, 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 the calling method is the same:
Copy Code code as follows:

$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.

Implementing closures
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

Copy Code code as follows:

Example One
Define an anonymous function in the function, and call it
function Printstr () {
$func = function ($str) {
Echo $str;
};
$func (' some string ');
}

Printstr ();

Case 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 Three
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 kind of writing may be familiar
Callfunc (function ($STR) {
Echo $str;
} );


Keywords to connect closures and external variables: 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:

Copy Code code as follows:

function Getmoney () {
$RMB = 1;
$dollar = 6;
$func = function () use ($RMB) {
Echo $RMB;
Echo $dollar;
};
$func ();
}

Getmoney ();

Output:
1
Error, unable to find dorllar variable


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:

Copy Code code as follows:

function Getmoney () {
$RMB = 1;
$func = function () use ($RMB) {
Echo $RMB;
Add the value of $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:

Copy Code code as follows:

function Getmoney () {
$RMB = 1;
$func = function () use (& $RMB) {
Echo $RMB;
Add the value of $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:

Copy Code code as follows:

function Getmoneyfunc () {
$RMB = 1;
$func = function () use (& $RMB) {
Echo $RMB;
Add the value of $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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.