PHP functions Call_user_func and Call_user_func_array detailed

Source: Internet
Author: User
PHP functions Call_user_func and Call_user_func_array detailed

The Call_user_func function is similar to a particular method of calling a function, using the following method:?
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:
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:
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.

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:
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 parameters

Description

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:

?

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 argument 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? Maybe you can understand that Because of the existence of this function, I find that the function overload can also be used:

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);
?

?

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