I wrote a previous article about the use of closures( Click here to enter ), this in-depth summary of the usage and understanding of anonymous functions in php:
Anonymous functions in php, also called closure functions, allow you to specify a function without a name. Assign anonymous functions to variables and call them through variables. For example:
<?php $anonymousFunc = function($username){ echo $username; }; $anonymousFunc("Qiao Feng!");
Tip 1: put the anonymous function in the ordinary function, or return the anonymous function, which forms a simple closure
<?php function closureFunc(){ $anonymousFunc = function(){ echo "Qiao Feng!"; }; $anonymousFunc();//Anonymous function called inside normal function } closureFunc();//output: Qiao Feng
Tip 2: reference local variables in anonymous functions (here you need to refer to a php keyword use)
<?php function closureFunc(){ $username = 'Qiao Feng'; $anonymousFunc = function() use($username){ echo $username; }; $anonymousFunc();//Anonymous function called here } closureFunc();//output: Qiao Feng
Skill 3: return anonymous function in normal function
<?php function closureFunc(){ $username = 'Qiao Feng'; $anonymousFunc = function() use($username){ echo $username; }; return $anonymousFunc;// Function returns an anonymous function } $func = closureFunc(); $func(); //Then call $func()
4 return anonymous function and pass parameter to anonymous function
<?php function closureFunc(){ $username = 'Qiao Feng'; $anonymousFunc = function($lover,$skill) use($username){ echo $username.$lover.$skill; }; return $anonymousFunc; } $func = closureFunc(); $func("Arzhu","Dragons");//Qiao Feng, a Zhu, dragon catcher
5 use closure to change the variable value of context reference
<?php function closureFunc(){ $number = 100; $anonymousFunc = function() use($number) { $number++; echo $number.PHP_EOL; }; echo $number.PHP_EOL; return $anonymousFunc; } $func = closureFunc();// Output 1 here, directly calling the echo $number.PHP_EOL; That is 100 $func();// Return value of calling function $anonymousFunc $number++ That is 101 $func(); //101 $func();//101
In the above input result, it is found that the following two FuncS () return 101, and the value has not changed. If you want to accumulate the + + effect, you only need to add a & reference symbol (changes in anonymous functions will also affect external variables). Modify the following:
<?php function closureFunc(){ $number = 100; $anonymousFunc = function() use(&$number) { $number++; echo $number.PHP_EOL; }; echo $number.PHP_EOL; return $anonymousFunc; } $func = closureFunc();// Output 1 here, directly calling the echo $number.PHP_EOL; That is 100 $func();// Return value of calling function $anonymousFunc $number++ That is 101 $func(); //102 $func();//103
Tip 6: pass anonymous functions as parameters
<?php //Define common functions, anonymousFunc Is a parameter variable function myFunc($anonymousFunc){ $anonymousFunc("Qiao Feng"); } myFunc(function($username){ //Here we call ordinary functions and pass anonymous functions as parameters to myFunc Medium $anonymousFunc echo $username; });//Output Qiaofeng
At present, this is all you need to do. Welcome to add...