Detailed Explanation of PHP Anonymous Function of Closure Function

  • 2021-11-29 23:25:12
  • OfStack

In PHP, the anonymous function (Anonymous functions), also known as the closure function (closures), allows the temporary creation of a function without a specified name. It is often used as an argument to the callback function (callback). Of course, there are other applications.

Note: php closures are available after PHP 5.3

What is a closure? Closures are blocks of code that can contain free (unbound to specific objects) variables; These variables are not defined within this code block or in any global context, but are defined in the environment in which the code block is defined (local variables). The word "closure" 1 comes from the combination of the code block to be executed (since 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 the binding for the free variables. In the field of programming, we can say that a child function can use local variables in the parent function, which is called closure.

PHP anonymous functions and closures use the same syntax as ordinary functions, but anonymous functions and closure numbers are objects disguised as functions.

Anonymous functions: Functions that do not have a name. Anonymous functions can be assigned to variables and passed by objects. However, anonymous functions are still functions, so they can be called and arguments can be passed in. Anonymous functions are especially suitable as callbacks to functions or methods.

Closure: A function that encapsulates the surrounding state when it is created. Even if the environment in which the closure is located no longer exists, the state encapsulated 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 for closures is fairly simple, and the only keywords to note are use, which connects closures and external variables.


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

Here are a few examples of implementing closures:


// Example 1 Pass an anonymous function as an argument and call it 
function callFunc( $func ) {
  $func( "some string\r\n" );
}
$printStrFunc = function( $str ) {
  echo $str;
};

// Example 2 You can also pass anonymous functions directly. If you understand js This writing may be familiar 
callFunc( $printStrFunc );
callFunc( function( $str ) {
  echo $str;
} );

// Example 3  Keywords for concatenating closures and external variables: USE
function getMoney() {
 $rmb = 1;
 $dollar = 6;
 $func = function() use ( $rmb ) {
  echo $rmb;
  echo $dollar;
 };
 $func();
}
getMoney();
// Output: 1
// Error reported, can not be found dorllar Variable 

// Example 4 Variables that change context in anonymous functions 
function getMoney() {
  $rmb = 1;
  $func = function() use ( &$rmb ) {
   echo $rmb . "<br>";
     // Put $rmb Add the value of 1
   $rmb++;
 };
 $func();
 echo $rmb;
}
getMoney();
// Output: 
//1
//2

Although the syntax and implementation of closures are very simple, it is not easy to use them well.

Making good use of closures can help us

1 code to reduce the loop of foreach 2 Reduce the parameters of the function 3 Release the recursive function

Summarize


Related articles: