PHP functions call_user_func and call_user_func_array usage details _ PHP Tutorial

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

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.