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 solitary function:
Copy CodeThe code is as follows: function Funa ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func (' Funa ', "111", "222");
Call_user_func (' Funa ', "333", "444");
Display 111 222 333 444
Have you noticed that this usage is a bit like the call method in JavaScript, hey
?>
The second is to invoke functions inside the class:
Copy CodeThe code is as follows: 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 your own to see what the results are ~ hehe ~ Hey, hint. The Func_get_args () function is to get the arguments passed into the function, return an array, and the Func_num_args () function gets the number of arguments in the passed function.
Let's take a look at the Call_user_func_array function.
This function is also used when the function needs to be called dynamically, and its usage and call_user_func function are more like, except that the parameter is passed in an array.
Copy the Code code as follows: function A ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func_array (' A ', Array ("111", "222"));
Showing 111 of 222
?>
The Call_user_func_array function can also invoke methods within the class.
Copy the Code code as follows: Class ClassA
{
function BC ($b, $c) {
$BC = $b + $c;
Echo $BC;
}
}
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("111″," 222″));
Showing 333
?>
Let's look at an example of a dynamic call function:
Copy the Code code as follows: 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");
?>
http://www.bkjia.com/PHPjc/736829.html www.bkjia.com true http://www.bkjia.com/PHPjc/736829.html techarticle The call_user_func function is used when a function needs to be called dynamically, and this function has two uses: the first is to invoke the solitary function: the copy code is as follows: Php function Fu ...