PHP Closure (Closure)

Source: Internet
Author: User
A preliminary study of PHP Closure (Closure) discovered that PHP has come out of version 5.5, and I have been using PHP5.2. it makes me look like a young man from the mountains, and the soil is falling behind. After I used to use closures in javascript, I suddenly became interested in the closure of PHP.

Therefore, a WAMP integrated development environment is deployed on the Internet, which is in PHP5.3 (PHP5.3 introduced the closure feature). It is really convenient to install and use WAMP. After a simple configuration, start.

Anonymous functions

When it comes to closures, you have to think of anonymous functions, also called closures. it seems that PHP closures are mainly implemented by them. Declare an anonymous function as follows:

$ Func = function () {}; // with the Terminator

As you can see, the anonymous function has no name. to use it, you need to return it to a variable. Anonymous functions can declare parameters like normal functions, and the call methods are the same:

$ Func = function ($ param) {echo $ param;}; $ func ('Some string'); // output: // some string

By the way, before PHP introduces closures, there is also a function that can create anonymous functions: create function, but the code logic can only be written as strings, which looks obscure and difficult to maintain, so few people use it.

Implement closures

Passing in an anonymous function as a parameter in a common function can also be returned. This implements a simple closure.

There are three examples below:

// Example 1 // define an anonymous function in the function and call the function printStr () {$ func = function ($ str) {echo $ str ;}; $ func ('Some string');} printStr (); // Example 2 // return the anonymous function in the function and call it function getPrintStrFunc () {$ func = function ($ str) {echo $ str ;}; return $ func ;}$ printStrFunc = getPrintStrFunc (); $ printStrFunc ('Some string '); // Example 3 // pass the anonymous function as a parameter and call it function callFunc ($ func) {$ func ('Some string');} $ printStrFunc = function ($ str ) {Echo $ str ;}; callFunc ($ printStrFunc); // You can also pass anonymous functions directly. If you know about js, you may be familiar with callFunc (function ($ str) {echo $ str ;});

Keyword for connecting closure and external variables: USE

The closure can save some variables and values in the context of the code block. By default, an anonymous function cannot call the context variable of the code block, but must use the use keyword.

Let's look at another example:

Function getMoney () {$ RMB = 1; $ dollar = 6; $ func = function () use ($ RMB) {echo $ RMB; echo $ dollar ;}; $ func ();} getMoney (); // output: // 1 // error. the dorllar variable cannot be found.

As you can see, dollar is not declared in the use keyword and cannot be obtained in this anonymous function. therefore, pay attention to this issue during development.

Some people may think about whether the context variables can be changed in anonymous functions, but I find it is not possible:

Function getMoney () {$ RMB = 1; $ func = function () use ($ RMB) {echo $ RMB; // add the $ RMB value to 1 $ RMB ++;}; $ func (); echo $ RMB;} getMoney (); // output: // 1 // 1

Ah, the original use references only a copy of the variable. But I want to completely reference the variable, instead of copying it.

To achieve this effect, you can add an & symbol before the variable:

Function getMoney () {$ RMB = 1; $ func = function () use (& $ RMB) {echo $ RMB; // add the $ RMB value to 1 $ RMB ++;}; $ func (); echo $ RMB;} getMoney (); // output: // 1 // 2

Okay, so that the anonymous function can reference the context variable. If an anonymous function is returned to the outside world, the anonymous function will save the variables referenced by use, and the outside world will not be able to get these variables, so that the concept of 'closures 'may be clearer.

Change the preceding example according to the description:

Function getMoneyFunc () {$ RMB = 1; $ func = function () use (& $ RMB) {echo $ RMB; // add the $ RMB value to 1 $ RMB ++;}; return $ func;} $ getMoney = getMoneyFunc (); $ getMoney (); $ getMoney (); // output: // 1 // 2 // 3

Summary

The features of PHP closures are not surprising. In fact, using CLASS can achieve similar or even more powerful functions, and cannot be compared with JavaScript closures. we can only look forward to improving the closure support of PHP in the future. However, anonymous functions are quite useful. for example, if you use functions such as preg_replace_callback, you do not need to declare callback functions externally.

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.