Call_user_func functions and Call_user_func_array functions in PHP

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.