zoukankan      html  css  js  c++  java
  • call_user_func() 函数 用法

    call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )
    http://justcoding.iteye.com/blog/650843
    调用第一个参数所提供的用户自定义的函数。
    返回值:返回调用函数的结果,或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. ?>  
     
     
     
     
     
     
  • 相关阅读:
    百分点零售行业大数据解决方案
    百分点用户标签管理系统
    百分点个性化系统解决方案
    百分点数据洞察系统解决方案
    来学学数据分析吧(二)第一章 预测和关联数量特征
    来学学数据分析吧(一)
    生产者和消费者问题学习以及Java实现
    java中的双重锁定检查(Double Check Lock)
    python学习(十四)面向对象
    python学习(十二)模块
  • 原文地址:https://www.cnblogs.com/gzmg/p/3225504.html
Copyright © 2011-2022 走看看