PHP functions Call_user_func and Call_user_func_array usage _php tutorials

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

  • 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.