Call_user_func and call_user_func_array Functions

Source: Internet
Author: User

Today, by accident, I again saw the call_user_func_array () function in php in the official CodeIgniter document. I also encountered this function in the discuz Forum code last year, I checked the usage of this function on the internet today. I would like to share with you the usage of this call_user_func_array and call_user_func functions. In addition, I will provide the usage instructions for func_get_args () and func_num_args () functions.

Call_user_func is used when a function needs to be called dynamically. This function has two usage methods, which are described below:

(1) Call the lonely function:

<? Phpfunction funa ($ B, $ c) {echo $ B; echo $ c;} call_user_func ('funa', "111", "222"); call_user_func ('funa ', "333", "444"); // result output: 111 222 333 444 // have you found that this usage is a bit like the call method in javascript?>

(2) call internal functions of the class:

<?phpclass a{function b(){$args=func_get_args();$num=func_num_args();print_r($args);echo $num;}}call_user_func(array("a","b"),"111","222");?>

In the preceding example, the result is as follows:

Array
(
[0] = & gt; 111
[1] => 222.
)
2

Tip: The func_get_args () function is used to obtain the parameters passed in to the function. An array is returned. The func_num_args () function gets the number of parameters in the input function.

Next let's take a look at call_user_func_array. This function is also used when a function needs to be called dynamically. Its usage is similar to that of the call_user_func function, but the parameter is passed in an array. The example is as follows:

<? Phpfunction a ($ B, $ c) {echo $ B; echo $ c;} call_user_func_array ('A', array ("111", "222 ")); // result output: 111 222?>

The call_user_func_array function can also call internal methods of the class, such:

<? PhpClass ClassA {function bc ($ B, $ c) {$ bc = $ B + $ c; echo $ bc ;}} call_user_func_array (array ('classa ', 'bc'), array ("111", "222"); // result output: 333?>

The following is an example of dynamic function call:

<? Phpfunction 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 ("11"); otest ("11", "22"); otest ("11", "22 ", "33");?>

The readers of the above examples can be more impressed when they can run them on their own.

Articles you may be interested in
  • PHP filter_var () function Filter function
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Php prompts Call to undefined function curl_init () Error Solution
  • JavaScript array operation function Summary (push, pop, join, shift, unshift, slice, splice, concat)
  • Php uses the filter function to verify the mailbox, url, and IP address
  • PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
  • Fatal error Call to undefined function date_default_timezone_set ()

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.