call_user_function()方法的使用

來源:互聯網
上載者:User

標籤:style   http   color   使用   os   strong   io   art   

call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )

調用第一個參數所提供的使用者自訂的函數。
傳回值:返回調用函數的結果,或FALSE。

example:

Php代碼  
  1. <?php  
  2. function eat($fruit) //參數可以為多個  
  3. {  
  4.      echo "You want to eat $fruit, no problem";  
  5. }  
  6. call_user_func(‘eat‘, "apple"); //print: You want to eat apple, no problem;  
  7. call_user_func(‘eat‘, "orange"); //print: You want to eat orange,no problem;  
  8. ?>  
 


調用類的內部方法:

Php代碼  
  1. <?php  
  2. class myclass {  
  3.      function say_hello($name)  
  4.      {  
  5.          echo "Hello!$name";  
  6.      }  
  7. }  
  8.   
  9. $classname = "myclass";  
  10.   
  11. //調用類內部的函數需要使用數組方式 array(類名,方法名)  
  12. call_user_func(array($classname, ‘say_hello‘), ‘dain_sun‘);  
  13.   
  14. //print Hello! dain_sun  
  15.   
  16. ?>  
 

call_user_func_array 函數和 call_user_func 很相似,只是使用了數組的傳遞參數形式,讓參數的結構更清晰:

call_user_func_array ( callback $function , array $param_arr )

調用使用者定義的函數,參數為數組形式。
傳回值:返回調用函數的結果,或FALSE。

Php代碼  
  1. <?php  
  2.   
  3. function debug($var, $val)  
  4. {  
  5.      echo "variable: $var <br> value: $val <br>";  
  6.      echo "<hr>";  
  7. }  
  8.   
  9.   
  10. $host = $_SERVER["SERVER_NAME"];  
  11. $file = $_SERVER["PHP_SELF"];  
  12.   
  13. call_user_func_array(‘debug‘, array("host", $host));  
  14. call_user_func_array(‘debug‘, array("file", $file));  
  15.   
  16.   
  17. ?>  

 



調用類的內部方法和 call_user_func 函數的調用方式一樣,都是使用了數組的形式來調用。


exmaple:

Php代碼  
  1. <?php  
  2.   
  3. class test  
  4. {  
  5.       function debug($var, $val)  
  6.       {  
  7.           echo "variable: $var <br> value: $val <br>";  
  8.           echo "<hr>";  
  9.       }  
  10. }  
  11.   
  12. $host = $_SERVER["SERVER_NAME"];  
  13. $file = $_SERVER["PHP_SELF"];  
  14.   
  15. call_user_func_array(array(‘test‘, ‘debug‘), array("host", $host));  
  16. call_user_func_array(array(‘test‘, ‘debug‘), array("file", $file));  
  17.   
  18. ?>  

 



註:call_user_func函數和call_user_func_array函數都支援引用。

Php代碼  
    1. <?php  
    2. function increment(&$var)  
    3. {  
    4.     $var++;  
    5. }  
    6.   
    7. $a = 0;  
    8. call_user_func(‘increment‘, $a);  
    9. echo $a; // 0  
    10.   
    11. call_user_func_array(‘increment‘, array(&$a)); // You can use this instead  
    12. echo $a; // 1  
    13. ?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.