Explanation of Concept and Usage of PHP Callback Function and Anonymous Function
- 2021-09-12 00:46:06
- OfStack
In this paper, the concept and usage of PHP callback function and anonymous function are described with examples. Share it for your reference, as follows:
1. Callback function
In fact, the callback function of PHP and the callback function of C, Java and other languages have the same function as one model and one sample, which is suddenly jumping to execute the set callback function during the execution of the main thread;
After the callback function is executed, go back to the main thread to handle the next flow
When calling the callback function in php, instead of using the function name as the function parameter directly as in c and java, it is executed by using the string name corresponding to the function in php
1.1, No Parameter Callback
<?php
// Parameterless 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');
// Results
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, 1,2);
call_user_func_array($func, array(1,2));
// Results
1<====>2.
1<====>2.
1.3. Class Method and Static Method Callbacks
<?php
class Test{
// Member function
function callback($a,$b){
echo "callback $a<====>$b.<br/>";
}
public static function staticCallback($a,$b){
echo "staticCallback $a<====>$b.<br/>";
}
}
// Non-static method invocation mode 1
$test = new Test();
call_user_func(array($test, 'callback'), 1,2);
call_user_func_array(array($test, 'callback'), array(1,2));
// Non-static method invocation mode 2
$func = 'callback';
$test->$func(7,9);
// Static method calling mode
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.
callback 1<====>2.
callback 7<====>9.
staticCallback 4<====>6.
staticCallback 4<====>6.
staticCallback 4<====>6.
2. Anonymous functions
2.1 The anonymous function (Anonymous functions) in php, also known as the closure function (closures), allows you to specify a function without a name. The most commonly used is the parameter value of the callback function
<?php
$closureFunc = function($str){
echo $str.'<br/>';
};
$closureFunc("hello world!");
// Results
hello world!
2.2. Closure
2.2. 1, pass in parameters, 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");
// Results
lvfk-- Male --23
2.2. 2, return closure function
<?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();
// Results
closureFunc lvfk+++ Male
lvfk-- Male
lvfk-- Male
2.2. 3. The closure changes the value of the context and needs 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();
// Results
lvfk+++1
lvfk--2
lvfk--3
lvfk--4
So that's a simple application of closures, and you can see from closures that you use closures outside of a function, and the parameters that you pass in to the closures can actually be the context objects,
You can also change the context object value within a closure, but it must be passed by reference
For the role of closures, please refer to: https://www.ofstack.com/article/61261. htm
For more readers interested in PHP related contents, please check the special topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Complete Collection of Operation Skills of PHP Array (Array)", "Tutorial of PHP Data Structure and Algorithm" and "Summary of php Programming Algorithm"
I hope this article is helpful to everyone's PHP programming.