Call_user_func (Callback $ function [, mixed $ parameter [, mixed $...])
Call the user-defined function provided by the first parameter.
Return Value: return the result of calling the function, or false.
Example:
PHP code
- <? PHP
- Function eat ($ fruit) // you can specify multiple parameters.
- {
- Echo "you want to eat $ fruit, no problem ";
- }
- Call_user_func ('eid', "apple"); // print: You want to eat apple, no problem;
- Call_user_func ('eid', "orange"); // print: You want to eat orange, no problem;
- ?>
Internal method of the call class:
PHP code
- <? PHP
- Class myclass {
- Function say_hello ($ name)
- {
- Echo "Hello! $ Name ";
- }
- }
- $ Classname = "myclass ";
- // Use array (Class Name, method name) to call functions inside the class)
- Call_user_func (Array ($ classname, 'Say _ hello'), 'dain _ Sun ');
- // Print hello! Dain_sun
- ?>
The call_user_func_array function is similar to call_user_func,Array UsedTo make the parameter structure clearer:
Call_user_func_array(Callback $ function, array $ param_arr)
Call a user-defined function. The parameter format is array.
Return Value: return the result of calling the function, or false.
PHP code
- <? PHP
- Function debug ($ var, $ Val)
- {
- Echo "variable: $ var <br> value: $ Val <br> ";
- Echo "<HR> ";
- }
- $ Host = $ _ server ["SERVER_NAME"];
- $ File = $ _ server ["php_self"];
- Call_user_func_array ('debug', array ("host", $ host ));
- Call_user_func_array ('debug', array ("file", $ file ));
- ?>
The internal method of the call class is the same as that of the call_user_func function. It is called using arrays.
Exmaple:
PHP code
- <? PHP
- Class Test
- {
- Function debug ($ var, $ Val)
- {
- Echo "variable: $ var <br> value: $ Val <br> ";
- Echo "<HR> ";
- }
- }
- $ Host = $ _ server ["SERVER_NAME"];
- $ File = $ _ server ["php_self"];
- Call_user_func_array (Array ('test', 'debug'), array ("host", $ host ));
- Call_user_func_array (Array ('test', 'debug'), array ("file", $ file ));
- ?>
Note: call_user_func and call_user_func_array Functions Support reference.
PHP code
- <? PHP
- Function increment (& $ var)
- {
- $ Var ++;
- }
- $ A = 0;
- Call_user_func ('credentials', $ );
- Echo $ A; // 0
- Call_user_func_array ('secret', array (& $ A); // you can use this instead
- Echo $ A; // 1
- ?>