Php Closure anonymous functions

Source: Internet
Author: User
This article mainly introduces the PHP anonymous function introduced by php5.3, that is, the function of the Closure (Closure) and the function of the Closure, which is very detailed. we recommend it to friends who need it. 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 () {}; // you can see that an anonymous function has no name. if you want to use it, you need to return it to a variable. Anonymous functions can declare parameters like normal functions, and 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.

1. 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); // Anonymous functions can also be passed directly. If you know about js, you may be familiar with callFunc (function ($ str) {echo $ str ;});

2. Keywords 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.

Do you have a new understanding of PHP anonymous functions, that is, closure functions? I hope this article will give you some tips and hope you will like them.

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.