1. Callback function
In fact, the callback function of PHP is exactly the same as the function of the callback function of C, Java and so on, all of which are in the process of executing the main thread and suddenly jump to execute the set callback function;
After the callback function is executed, go back to the main thread to process the next process
Instead of using the function name as a function parameter in PHP, like C and Java, the callback function is executed in PHP using the corresponding string name of the function.
1.1. No parameter callback
<?php//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 ');//result ecute main start.execute no parameters callback.execute main end.
1.2. Global callback function
<?php//Global function callback function callback ($a, $b) { echo "$a <====> $b .<br/>";} $func = ' callback '; Call_user_func ($func), Call_user_func_array ($func, array);//Results 1<====>2.1<==== >2.
1.3. Class method and static method callback
<?phpclass test{ //member functions function callback ($a, $b) { echo "callback $a <====> $b .<br/>"; } public static function Staticcallback ($a, $b) { echo "Staticcallback $a <====> $b .<br/>"; }} The non-static method calls a $test = new test (), Call_user_func (Array ($test, ' callback '), and Call_user_func_array (Array ($test, ' Callback '), array, or//non-static method call mode Two $func = ' callback '; $test-$func (7,9);//static method call mode Call_user_func (' 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.callback 1<====>2.callback 7<====>9. Staticcallback 4<====>6.staticcallback 4<====>6.staticcallback 4<====>6.
2. anonymous function
The concept and usage of PHP callback function and anonymous function