PHP custom Functions Call_user_func and Call_user_func_array detailed _php tutorials

Source: Internet
Author: User
The Call_user_func function is similar to a particular method of calling a function, using the following method:
Copy CodeThe code is as follows:
function A ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func (' A ', "111", "222");
Call_user_func (' A ', "333", "444");
Display 111 222 333 444
?>

Call the method inside the class is rather strange, incredibly using an array, do not know how developers think, of course, save new, is full of innovative:
Copy CodeThe code is as follows:
Class A {
Function B ($c)
{
Echo $c;
}
}
Call_user_func (Array ("A", "B"), "111");
Showing 111
?>

The Call_user_func_array function is similar to the Call_user_func, except that the parameter is passed in a different way, allowing the structure of the parameter to be clearer:
Copy CodeThe code is 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 CodeThe 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"));
Showing 333
?>

Both the Call_user_func function and the Call_user_func_array function support references, which makes them more functionally consistent with normal function calls:
Copy CodeThe code is as follows:
Function A (& $b)
{
$b + +;
}
$c = 0;
Call_user_func (' A ', & $c);
echo $c;//Display 1
Call_user_func_array (' A ', Array (& $c));
echo $c;//Display 2

Simple usage of PHP's Call_user_func_array
Today in the group, there is a call Lewis in the use of the Call_user_func_array, because it has not been used, and can not say anything, so look at the handbook, found that this is written:
Call_user_func_array
(PHP 4 >= 4.0.4, PHP 5)
Call_user_func_array--Call a user function given with an array of parametersdescription
Mixed Call_user_func_array (callback function, array Param_arr)
Call a user defined function given by function with the parameters in Param_arr.
Then there is an example:
Copy CodeThe code is as follows:
function Foobar ($arg, $arg 2) {
Echo __function__, "Got $arg and $arg 2\n";
}
class Foo {
function Bar ($arg, $arg 2) {
Echo __method__, "Got $arg and $arg 2\n";
}
}
Call the Foobar () function with 2 arguments
Call_user_func_array ("Foobar", Array ("One", "one"));
Call the $foo->bar () method with 2 arguments
$foo = new Foo;
Call_user_func_array (Array ($foo, "bar"), Array ("Three", "four");
?>

The output of the above routines is similar to the following:
Foobar got one and
Foo::bar got three and four
Example #2 Call_user_func_array () using namespace name
Copy CodeThe code is as follows:
namespace Foobar;
Class Foo {
static public Function test ($name) {
Print "Hello {$name}!\n";
}
}
As of PHP 5.3.0
Call_user_func_array (__namespace__. ' \foo::test ', Array (' Hannes '));
As of PHP 5.3.0
Call_user_func_array (Array (__namespace__. ' \foo ', ' Test '), Array (' Philip ');
?>

The output of the above routines is similar to the following:
Hello hannes!
Hello philip!
Example #3 Using lambda function
Copy CodeThe code is as follows:
$func = function ($arg 1, $arg 2) {
return $arg 1 * $arg 2;
};
Var_dump (Call_user_func_array ($func, Array (2, 4))); /* As of PHP 5.3.0 */
?>

The above routines will output:
Int (8)
Do you think it's a little clear after the example?
I myself understand this function, if the wrong, but also hope that you master do not ridicule:
The real usage of this function is a bit like function overloading, because his first argument is a character type, that is, the name of the function, the second parameter is an array, we can think of it as the parameters of the function, and in fact it is so used, if you read my previous article: PHP pseudo-overloaded, perhaps you can understand, Because of the existence of this function, I find that the function overload can also be used:
Copy CodeThe code is as follows:
/**
* After writing the example, it was supposed to be done, the results met someone asked Call_user_func_array (), read the manual
* Originally, the test function above me can also be reduced to the following example,
*/
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);

Do you see? And my original writing, in the PHP pseudo-overloaded article mentioned, only for reference ....

http://www.bkjia.com/PHPjc/323837.html www.bkjia.com true http://www.bkjia.com/PHPjc/323837.html techarticle The Call_user_func function is similar to a special method of calling a function, using the following method: Copy code code as follows: function A ($b, $c) {echo $b; echo $c;} call_user_func (' A ', "111 ...

  • Related Article

    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.