PHP functions call_user_func and call_user_func_array

Source: Internet
Author: User
Let's share with you the usage of the call_user_func_array and call_user_func functions. The func_get_args () function and func_num_args () function are also provided !!

Let's share with you the usage of the call_user_func_array and call_user_func functions. The func_get_args () function and func_num_args () function are also provided !!

Call_user_func function is used only when the 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 parameter is passed into 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 ");
?>

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.