Php functions call_user_func and call_user_func_array

Source: Internet
Author: User
The call_user_func and call_user_func_array functions of php are described as follows :? Functiona ($ B, $ c )? {Echo $ B; echo $ c;} call_user_func ('A', "111" php functions call_user_func and call_user_func_array

The call_user_func function is similar to a special method for calling a function. the usage 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
?>

The internal method of calling the class is strange. the method actually uses an array and does not know how developers think about it. of course, the new method is saved, which is also innovative:
Class {
Function B ($ c )?
{
Echo $ c;
}
}
Call_user_func (array ("a", "B"), "111 ");
// Display 111
?>

The call_user_func_array function is similar to call_user_func, but the parameter is passed in another way to make the parameter structure clearer:
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.

Class ClassA
{

Function bc ($ B, $ c ){
???? $ Bc = $ B + $ c;
Echo $ bc;
}
}
Call_user_func_array (array ('classa ', 'BC'), array ("111", "222 "));

// Display 333
?>

Call_user_func and call_user_func_array functions support reference, which makes them more functional than 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 call_user_func_array in php

Today, in the group, a user named lewis asked call_user_func_array for usage. because he had never used it before and could not say anything, he looked at the manual and found that it was written like this:

Call_user_func_array

(PHP 4> = 4.0.4, PHP 5)

Call_user_func_array -- Call a user function given with an array of parametersDescriptionmixed? Call_user_func_array? (Callback function, array param_arr)

Call a user defined function given?Function, With the parameters in?Param_arr.?
There is another example:

?

I believe I should understand the example after reading it?
I understand this function in this way. if not, I hope you will not laugh at it:
???? The actual usage of this function is a bit similar to the function overload, because its first parameter is of the struct type, that is, the function name, and the second parameter is an array, we can use this function as a parameter. In fact, this is the case. if you have read my previous article: PHP pseudo-heavy load ?, Perhaps you can understand that because of the existence of this function, I found that function overloading can also be used as follows:

Function otest1 ($ a) {echo ('one parameter');} function otest2 ($ a, $ B) {echo ('Two parameters ');} function otest3 ($ a, $ B, $ c) {echo ('three thes');} function otest () {$ args = func_get_args (); $ num = func_num_args (); call_user_func_array ('test '. $ num, $ args);} otest (1, 2 );
?

?

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.