Analysis of calluserfunc () function in PHP and how to use calluserfunc to call user-defined functions

Source: Internet
Author: User
You can use the call_user_func function to call a user-defined function by passing in the string function, and reference is supported. This function allows users to call directly written functions and input certain parameters. The following describes how to use this function. For more information, see the UCenter source code function call_user_func, I thought it was a self-defined function, and the results were not found everywhere. Later I learned that call_user_func is a built-in function of PHP. This function allows users to call directly written functions and input certain parameters. The following describes how to use this function.

The call_user_func function is similar to a special method for calling a function. the usage is as follows:

<? Phpfunction nowamagic ($ a, $ B) {echo $ a; echo $ B;} call_user_func ('nowamagic ', "", ""); call_user_func ('nowamagic ', "", ""); // Display?>

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 quite innovative:

<? Phpclass a {function B ($ c) {echo $ c ;}} call_user_func (array ("a", "B"), ""); // Display?>

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:

<? Phpfunction a ($ B, $ c) {echo $ B; echo $ c;} call_user_func_array ('A', array ("", ""); // Display?>

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

<? PhpClass ClassA {function bc ($ B, $ c) {$ bc = $ B + $ c; echo $ bc ;}} call_user_func_array (array ('classa ', 'BC'), array ("", ""); // Display?>

Call_user_func and call_user_func_array functions support reference, which makes them more functional than normal function calls:

<? Phpfunction a ($ B) {$ B ++;} $ c =; call_user_func ('A', $ c); echo $ c; // Display call_user_func_array ('A ', array ($ c); echo $ c; // Display?> In addition, call_user_func and call_user_func_array functions support reference. View sourceprint? <? Phpfunction increment (& $ var) {$ var ++;} $ a =; call_user_func ('credentials', $ a); echo $ a; // call_user_func_array ('credentials ', array (& $ a); // You can use this insteadecho $ a; //?>

The following describes how to use call_user_func to call a user-defined function.

You can use the call_user_func function to call a user-defined function by passing in the string function, and reference is supported.

1. mixed call_user_func (callable $ callback [, mixed $ parameter [, mixed $...])

Call the UDF provided by the first parameter. the following parameter is the UDF parameter and the result of the UDF is returned.

Function say ($ word) {echo $ word;} call_user_func ('say', 'Hello World'); // hello world, of course, you can also call methods in the class: class A {function say ($ word = '') {echo $ word ;}}$ a = new A (); // note that it must be instantiated, unless it is staticcall_user_func (array ($ a, 'say'), 'Hello World'); // hello world

2. mixed call_user_func_array (callable $ callback, array $ param_arr)

The functions of call_user_func_array and call_user_func functions are the same, but they are slightly different when calling parameters:

Function a ($ word) {echo $ word;} class A {function say ($ word = '') {echo $ word;} call_user_func_array ('A ', array ('Hello World'); // hello world $ a = new A (); // note that it must be instantiated unless it is staticcall_user_func_array (array ($, 'say'), array ('Hello World'); // hello world

Note: The other two functions are call_user_method and call_user_method_array (). However, they are disabled after PHP4.1.

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.