php学习之道:call_user_func和call_user_func_array的用法
来源:互联网 发布:穿过的内衣裤出售 淘宝 编辑:程序博客网 时间:2024/05/22 15:31
call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )
调用第一个参数所提供的用户自定义的函数。
返回值:返回调用函数的结果,或FALSE。
example :
- <?php
- function eat($fruit) //参数可以为多个
- {
- echo "You want to eat $fruit, no problem";
- }
- call_user_func('eat', "apple"); //print: You want to eat apple, no problem;
- call_user_func('eat', "orange"); //print: You want to eat orange,no problem;
- ?>
调用类的内部方法:
- <?php
- class myclass {
- function say_hello($name)
- {
- echo "Hello!$name";
- }
- }
- $classname = "myclass";
- //调用类内部的函数需要使用数组方式 array(类名,方法名)
- call_user_func(array($classname, 'say_hello'), 'dain_sun');
- //print Hello! dain_sun
- ?>
call_user_func_array 函数和 call_user_func 很相似,只是 使 用了数组 的传递参数形式,让参数的结构更清晰:
call_user_func_array ( callback $function , array $param_arr )
调用用户定义的函数,参数为数组形式。
返回值:返回调用函数的结果,或FALSE。
- <?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));
- ?>
调用类的内部方法和 call_user_func 函数的调用方式一样,都是使用了数组的形式来调用。
exmaple:
- <?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));
- ?>
注:call_user_func 函数和call_user_func_array函数都支持引用。
- <?php
- function increment(&$var)
- {
- $var++;
- }
- $a = 0;
- call_user_func('increment', $a);
- echo $a; // 0
- call_user_func_array('increment', array(&$a)); // You can use this instead
- echo $a; // 1
- ?>
0 0
- php学习之道:call_user_func和call_user_func_array的用法
- php之call_user_func();和call_user_func_array();
- 【php】php中call_user_func_array和call_user_func函数的用法
- PHP call_user_func与call_user_func_array函数的用法
- call_user_func、call_user_func_array的用法
- PHP之call_user_func函数和call_user_func_array函数
- call_user_func 和call_user_func_array 简单的用法
- call_user_func和call_user_func_array的用法详解
- PHP 函数之 call_user_func & call_user_func_array
- PHP 中的call_user_func_array() 和call_user_func()
- 【转】php中call_user_func()函数和call_user_func_array()函数的用法详解
- PHP回调函数call_user_func()和call_user_func_array()的使用
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- HDOJ 3518 Boring counting
- jsoncpp 简单案例
- Java集合框架总结(3)——TreeSet类的排序问题
- hadoop 我的总结
- Linux上安装MySQL5.6
- php学习之道:call_user_func和call_user_func_array的用法
- java实例2
- Java集合框架总结(4)——List接口的使用
- 雾山的Android学习笔记---消息机制(三) .
- cocos项目导入其他源文件时添加依赖库时,头文件提示找不到目录中的文件
- 求二叉树中节点的最大距离
- Ubuntu下crontab命令的用法
- Java集合框架总结(5)——Map接口的使用
- mailcore(一)