Anonymous functions (Anonymous functions), also called closures, allow temporary creation of a function without a specified name. Often used as a parameter to callback functions. Of course, there are other applications.
Note: The php closure is only available after PHP5.3 version
What is a closure? A closure is a block of code that can contain free (not bound to a specific object) variable; these variables are not defined within this block of code or in any global context, but are defined in the environment where the block of code is defined (local variables). The term "closure" comes from a combination of the code block to be executed (because free variables are contained in the code block, these free variables and the objects they refer to are not released) and the computing environment (scope) that provides bindings for free variables. In the field of programming, we can commonly say that child functions can use local variables in parent functions, and this behavior is called closure.
PHP anonymous functions and closures use the same syntax as ordinary functions, but the anonymous functions and closure numbers are actually objects disguised as functions.
Anonymous functions:It is a function without a name. Anonymous functions can be assigned to variables and passed to objects. However, anonymous functions are still functions, so they can be called and parameters can be passed into. Anonymous functions are particularly suitable as callbacks for functions or methods.
Closure:It refers to a function that encapsulates surrounding states when created. Even if the environment where the closure is located does not exist, the encapsulated state in the closure still exists.
Note: In theory, closures and anonymous functions are different concepts. However, PHP treats them as the same concept.
The syntax of closures is quite simple, and the only keywords to be paid attention to are use, which connects closures and external variables.
$a = function() use($b) { //TO-DO };
Here are some examples of implementing closures:
//Example 1: Pass anonymous function as a parameter and call itfunction callFunc( $func ) { $func( "some string\r\n" ); } $printStrFunc = function( $str ) { echo $str; };
//Example 2: Anonymous functions can also be passed directly. If you understand js, this writing may be very familiarcallFunc( $printStrFunc ); callFunc( function( $str ) { echo $str; } );
//Example 3: Keywords connecting closures and external variables: USEfunction getMoney() { $rmb = 1; $dollar = 6; $func = function() use ( $rmb ) { echo $rmb; echo $dollar; }; $func(); } getMoney(); //Output: 1//Report an error,Not founddorllarvariable
//Example 4: Change the context variable in anonymous functionfunction getMoney() { $rmb = 1; $func = function() use ( &$rmb ) { echo $rmb . "<br>"; //Add $rmb value by 1 $rmb++; }; $func(); echo $rmb; } getMoney(); //Output://1 //2
Although the syntax and implementation of closures are very simple, they are not easy to use well.
Use the closure well, can help us
- 1. Code to reduce the loop of foreach
- 2 Reduce the function's parameters
- 3. Unrestoring the recursive function
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below