The use of the Call_user_func_array and Call_user_func functions in PHP, plus a description of the usage of the Func_get_args () function and the Func_num_args () function.
The Call_user_func function is used when the function needs to be called dynamically, and this function has two uses, as explained below:
(1) Call the function of loneliness:
function Funa ($b, $c) {
Echo $b;
Echo $c;
}
Call_user_func (' Funa ', "111", "222");
Call_user_func (' Funa ', "333", "444");
Result output: 111 222 333 444
Have you noticed that this usage is a bit like the call method in JavaScript
?>
(2) Calling functions inside the class:
Class a{
Function B () {
$args =func_get_args ();
$num =func_num_args ();
Print_r ($args);
Echo $num;
}
}
Call_user_func (Array ("A", "B"), "111", "222");
?>
In the example above, the result output is as follows:
Array
(
[0] = 111
[1] = 222
)
2
Tip: the Func_get_args () function is to get the arguments passed into the function, returning an array, and the Func_num_args () function gets the number of arguments in the passed-in function.
Let's take a look at Call_user_func_array, which is also used when the function needs to be called dynamically, and its usage and CALL_USER_FUNC functions are compared, except that the parameters are passed in an array, as shown in the following example:
function A ($b, $c) {
echo $b; echo $c;
}
Call_user_func_array (' A ', Array ("111", "222"));
Result output: 111 222
?>
The Call_user_func_array function can also invoke methods inside the class, such as:
Class classa{
function BC ($b, $c) {
$BC = $b + $c; echo $bc;
}
}
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("111", "222"));
Result output: 333
?>
Let's look at an example of a dynamic call function:
function Otest1 ($a) {
Echo (' one parameter ');
}
function Otest2 ($a, $b) {
Echo (' two parameters ');
}
function Otest3 ($a, $b, $c) {
Echo (' three x ');
}
function Otest () {
$args =func_get_args ();
$num =func_num_args ();
Call_user_func_array (' otest '. $num, $args );
}
Otest ("11");
Otest ("11", "22");
Otest ("11", "22", "33");
?>
The above example readers will be more impressed when they run their own operations.