The usage of call_user_func and call_user_func_array in PHP functions is described in detail. Call_user_func is used only when a function needs to be called dynamically. this function has two usage methods: The first is to call a lonely function: Copy the code as follows :? The phpfunctionfu call_user_func function is used when a function needs to be called dynamically. this function has two usage methods:
The first is to call a lonely function:
The 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 found that this usage is a bit like the call method in javascript?
?>
The second is to call internal functions of the class:
The code is as follows:
Class {
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 above example, run the command to see what the result is ~ Hey hey ~ Note: The func_get_args () function is used to obtain the parameters passed in to the function and returns an array. the func_num_args () function gets the number of parameters in the input function.
Next let's take a look at the call_user_func_array function.
This function is also used when a function needs to be called dynamically. its usage is similar to that of the call_user_func function, but the input parameter is an array.
The code is as follows:
Function a ($ B, $ c)
{
Echo $ B;
Echo $ c;
}
Call_user_func_array ('A', array ("111", "222 "));
// Display 111 222
?>
The call_user_func_array function can also call internal methods of the class.
The code is as follows:
Class ClassA
{
Function bc ($ B, $ c ){
$ Bc = $ B + $ c;
Echo $ bc;
}
}
Call_user_func_array (array ('classa ', 'BC'), array ("111", "222 ″));
// Display 333
?>
The following is an example of dynamic function call:
The code is as follows:
Function otest1 ($)
{
Echo ('one parameter ');
}
Function otest2 ($ a, $ B)
{
Echo ('Two parameters ');
}
Function otest3 ($ a, $ B, $ c)
{
Echo ('three loan ');
}
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 first method of callback is to call a lonely function: the code is as follows :? Php function fu...