Call_user_function () method

Source: Internet
Author: User

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
  1. <? PHP
  2. Function eat ($ fruit) // you can specify multiple parameters.
  3. {
  4. Echo "you want to eat $ fruit, no problem ";
  5. }
  6. Call_user_func ('eid', "apple"); // print: You want to eat apple, no problem;
  7. Call_user_func ('eid', "orange"); // print: You want to eat orange, no problem;
  8. ?>
 


Internal method of the call class:

PHP code
  1. <? PHP
  2. Class myclass {
  3. Function say_hello ($ name)
  4. {
  5. Echo "Hello! $ Name ";
  6. }
  7. }
  8. $ Classname = "myclass ";
  9. // Use array (Class Name, method name) to call functions inside the class)
  10. Call_user_func (Array ($ classname, 'Say _ hello'), 'dain _ Sun ');
  11. // Print hello! Dain_sun
  12. ?>
 

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
  1. <? PHP
  2. Function debug ($ var, $ Val)
  3. {
  4. Echo "variable: $ var <br> value: $ Val <br> ";
  5. Echo "<HR> ";
  6. }
  7. $ Host = $ _ server ["SERVER_NAME"];
  8. $ File = $ _ server ["php_self"];
  9. Call_user_func_array ('debug', array ("host", $ host ));
  10. Call_user_func_array ('debug', array ("file", $ file ));
  11. ?>

 



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
  1. <? PHP
  2. Class Test
  3. {
  4. Function debug ($ var, $ Val)
  5. {
  6. Echo "variable: $ var <br> value: $ Val <br> ";
  7. Echo "<HR> ";
  8. }
  9. }
  10. $ Host = $ _ server ["SERVER_NAME"];
  11. $ File = $ _ server ["php_self"];
  12. Call_user_func_array (Array ('test', 'debug'), array ("host", $ host ));
  13. Call_user_func_array (Array ('test', 'debug'), array ("file", $ file ));
  14. ?>

 



Note: call_user_func and call_user_func_array Functions Support reference.

PHP code
    1. <? PHP
    2. Function increment (& $ var)
    3. {
    4. $ Var ++;
    5. }
    6. $ A = 0;
    7. Call_user_func ('credentials', $ );
    8. Echo $ A; // 0
    9. Call_user_func_array ('secret', array (& $ A); // you can use this instead
    10. Echo $ A; // 1
    11. ?>

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.