1. Callback functions
PHP callback functions and C, Java and other languages are exactly the same function, are in the main thread execution process, suddenly jump to execute the set callback function;
After the callback completes, it returns to the main thread for the rest of the process
Instead of using the function name as a function parameter, PHP uses the corresponding string name of the function
1.1. No parameter callback
<? Function callback(){echo 'execute no parameters callback.<br/>'; } function main($callback){ echo 'execute main start.<br/>'; $callback(); echo 'execute main end.<br/>'; } main('callback'); Ecute main start. execute no parameters callback. execute main end.Copy the code
1.2. Global callback functions
<? PHP / / global function callback function callback ($a, $b) {echo "$a $b. < = = = = > < br / >"; } $func = 'callback'; Call_user_func ($func, 1, 2); Call_user_func_array -- $func, array (1, 2)); Result 1<====>2. 1<====>2.Copy the code
1.3. Class and static method callbacks
<? Function callback($a,$b){echo "callback $a<====>$b. } public static function staticCallback($a,$b){ echo "staticCallback $a<====>$b.<br/>"; $test = new test (); Call_user_func (array ($test, 'callback'), 1, 2); Call_user_func_array -- array ($test, 'callback'), array (1, 2)); $func = 'callback'; $test - > $func (7, 9); Call_user_func (array('Test', 'staticCallback'), 4,6); Call_user_func_array -- array (' Test ', 'staticCallback), array (4, 6)); Call_user_func_array -- "Test: : staticCallback", array (4, 6)); / / results callback 1 < = = = = > 2. The callback 1 < = = = = > 2. 7 < = = = = > 9. The callback staticCallback 4 < = = = = > 6. StaticCallback 4 < = = = = > 6. staticCallback 4<====>6.Copy the code
2. Anonymous functions
2.1 Anonymous functions in PHP, also known as closures, allow you to specify a function without a name. The most common is the parameter value of the callback function
<? php $closureFunc = function($str){ echo $str.'<br/>'; }; $closureFunc("hello world!" ); // Result hello world!Copy the code
2.2, closures
2.2.1. Pass in parameters and reference local variables
<? PHP $closureFunc = function($name){$sex = 'male '; $func = function($age)use ($name,$sex){ echo "$name--$sex--$age<br/>"; }; $func(23); }; $func = $closureFunc("lvfk"); // result LVFK -- male --23Copy the code
2.2.2. Return closure functions
<? php $closureFunc = function($name){ echo 'closureFunc '; $sex = 'male '; echo "$name+++$sex<br/>"; $func = function()use ($name,$sex){ echo "$name--$sex<br/>"; }; return $func; }; $func = $closureFunc("lvfk"); $func(); $func(); ClosureFunc LVFK +++ male LVFK -- male LVFK -- maleCopy the code
2.2.3 Closures change context values and need to be passed by reference
<?php
$closureFunc = function($name){
$age = 1;
echo "$name+++$age<br/>";
$func = function()use ($name,&$age){
$age++;
echo "$name--$age<br/>";
};
return $func;
};
$func = $closureFunc("lvfk");
$func();
$func();
$func();
//结果
lvfk+++1
lvfk--2
lvfk--3
lvfk--4
Copy the code
So that’s a simple application of closures, where you can see that when you use a closure outside of a function, you pass in the contents of the closure’s parameters, which can actually be the contents of the context object,
Context object values can also be changed within closures, but must be reference-passed!