PHP Closure (Closure) usage details

Source: Internet
Author: User
This article introduces how to use the PHP Closure (Closure). If you need some help, you may find that PHP has already come out of version 5.5, but you have been using PHP5.2, it makes me look like a young man from the mountains, and the Earth 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:
The code is as follows:
$ Func = function (){

}; // With an 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:
The code is as follows:
$ 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:
The code is as follows:
// Example 1
// Define an anonymous function in the function and call it
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 );

// Anonymous functions can also be passed directly. If you know about js, you may be familiar with this method.
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:
The code is as follows:
Function getMoney (){
$ RMB = 1;
$ Dollar = 6;
$ Func = function () use ($ RMB ){
Echo $ RMB;
Echo $ dollar;
};
$ Func ();
}

GetMoney ();

// Output:
// 1
// Error. unable to find the dorllar variable

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:
The code is as follows:
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:
The code is as follows:
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:
The code is as follows:
Function getMoneyFunc (){
$ RMB = 1;
$ Func = function () use (& $ RMB ){
Echo $ RMB;
// Add the $ RMB value to 1
$ RMB ++;
};
Return $ func;
}

$ GetMoney = getMoneyFunc ();
$ GetMoney ();
$ 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.