PHP anonymous function (closure function) Detailed _php skills - PHP

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

In PHP function anonymous (Anonymous functions), also known as closures functions (closures), allow to create a temporary function name is not specified. Parameters often used as a callback function (callback) a. Of course, there are circumstances other applications.

Note: After php PHP5.3 closure is the only version

What is closure? Closures may comprise a free (not bound to a particular object) code block variable; or any of these variables are not defined in the global context in this block, but rather is defined (local variables) at ambient code definition block. "Closure" was coined by reference to the following two: a block of code to be executed (because the free variable is contained in the code block, the free variables and the object they refer to is not released), and to provide for the free variable bindings computing environment (scope). In the field of programming we can popular to say: Functions can use local variables in the parent function, this behavior is called closure.

PHP syntax anonymous functions and closures used in normal function the same, but the number of anonymous letters and closure package is actually disguised as a function of the object.

Anonymous function: that there is no function name anonymous functions can be assigned to variables, objects are passed but still anonymous function is a function, so you can call, you can also pass parameters anonymous function is particularly suitable as a callback function or method....

Closure: is a function of the state around the package when the closure created even where the environment does not exist, the state of the closure remains in the package.

Note: In theory, closures and anonymous functions are different concepts, however, PHP be regarded as the same concept.

The syntax closure is quite simple, the keyword should be noted that only use, use a connection closure and external variables.

$a = function() use($b) {
 //TO-DO
};

A few examples are given below to achieve closure:

// Example: The anonymous function as a parameter passing, and it calls the 
function callFunc ($ FUNC) { 
  $ FUNC ( "String some \ R & lt \ n-"); 
} 
$ printStrFunc = function (STR $) { 
  echo $ STR; 
} ;
// Example II: it can also be passed directly to the anonymous function. If you know js, such an approach may be very familiar 
callFunc ($ printStrFunc); 
callFunc (function ($ str) { 
  echo $ str; 
});
// Example Three: keywords and external connection closure variables: the USE 
function getMoney () { 
 $ = RMB. 1; 
 $ = Dollar. 6; 
 $ FUNC = function () use ($ RMB) { 
  echo $ RMB; 
  echo $ Dollar ; 
 }; 
 $ FUNC (); 
} 
getMoney (); 
// output: 1 
// error, can not find the variable dorllar
// Example 4: changing the variable context in the anonymous function 
function getMoney () { 
  $ = RMB. 1; 
  $ FUNC = function () use ($ & RMB) { 
   echo $ RMB "<br>";. 
     // The $ rmb value plus a 
   $ rmb ++; 
 }; 
 $ FUNC (); 
 echo $ rmb; 
} 
getMoney (); 
// output: 
// 1 
// 2

Although the closure syntax and implementation is very simple, but not easy to make good use of it.

Good use of closures, we can help

  • 1 is reduced foreach loop code
  • Reduction function parameter 2
  • 3 lifting recursive function

to sum up

That's all for this article, I hope the contents of this paper has some reference value of learning for all of us to learn or work, thank you for the support sensitive and eager Forum / Hi learning network. If you want to know more details, please see the related links below

The original address is: http: //www.piaodoo.com/thread-3557-1-1.html

Guess you like

Origin www.cnblogs.com/txdah/p/12093210.html