PHP functions Call_user_func and Call_user_func_array usage _php tips

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 lonely function:

Copy Code code as follows:
<?php
function Funa ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func (' Funa ', "111", "222");
Call_user_func (' Funa ', "333", "444");
displaying 111 222 333 444
Have you noticed that this usage is a bit like the call method in JavaScript, hehe
?>

The second is to invoke a function inside the class:
Copy Code code as follows:
<?php
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 it yourself to see what the result is ~ hehe ~ Hey, hint. The Func_get_args () function is to get the arguments passed into the function, return an array, and Func_num_args () function to get the number of arguments in the passed function.

Now let's look at the Call_user_func_array function.
This function is also used when a function needs to be invoked dynamically, and its usage is similar to the Call_user_func function, except that the arguments pass in an array.

Copy Code code as follows:
<?php
function A ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func_array (' A ', Array ("111", "222"));
Showing 111 222
?>

The Call_user_func_array function can also invoke methods within the class

Copy Code code as follows:
<?php
Class ClassA
{
function BC ($b, $c) {

$BC = $b + $c;
Echo $BC;
}

}
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("111″," 222″));
Show 333
?>


Let's look at an example of a dynamic call function:
Copy Code code as follows:
<?php
function Otest1 ($a)
{
Echo (' one parameter ');
}

function Otest2 ($a, $b)
{
Echo (' two parameters ');
}

function Otest3 ($a, $b, $c)
{
Echo (' three ");
}

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.