The Call_user_func function is used when you need to invoke a function dynamically, and this function has two uses:
The first is to invoke the lonely function:
Copy Code code as follows:
<?php
function Funa ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func (' Funa ', "111", "222");
Call_user_func (' Funa ', "333", "444");
displaying 111 222 333 444
Have you noticed that this usage is a bit like the call method in JavaScript, hehe
?>
The second is to invoke a function inside the class:
Copy Code code as follows:
<?php
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");
?>
The above example, run it yourself to see what the result is ~ hehe ~ Hey, hint. The Func_get_args () function is to get the arguments passed into the function, return an array, and Func_num_args () function to get the number of arguments in the passed function.
Now let's look at the Call_user_func_array function.
This function is also used when a function needs to be invoked dynamically, and its usage is similar to the Call_user_func function, except that the arguments pass in an array.
Copy Code code as follows:
<?php
function A ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func_array (' A ', Array ("111", "222"));
Showing 111 222
?>
The Call_user_func_array function can also invoke methods within the class
Copy Code code as follows:
<?php
Class ClassA
{
function BC ($b, $c) {
$BC = $b + $c;
Echo $BC;
}
}
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("111″," 222″));
Show 333
?>
Let's look at an example of a dynamic call function:
Copy Code code as follows:
<?php
function Otest1 ($a)
{
Echo (' one parameter ');
}
function Otest2 ($a, $b)
{
Echo (' two parameters ');
}
function Otest3 ($a, $b, $c)
{
Echo (' three ");
}
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");
?>