call_user_func和call_user_func_array的用法详解
来源:互联网 发布:淘宝开直通车有用吗 编辑:程序博客网 时间:2024/05/22 17:29
call_user_func和call_user_func_array的定义
mixed call_user_func ( callable $callback
[, mixed $parameter
[, mixed $...
]] )
Calls the callback given by the first parameter and passes the remaining parameters as arguments.
调用由第一参数给出的回调函数,并将其余的参数作为回调函数的参数。
mixed call_user_func_array ( callable $callback
, array $param_arr
)
Calls the callback given by the first parameter with the parameters in param_arr.
调用由第一参数给出的回调函数,并将数组param_arr的参数作为回调函数的参数。
Returns the return value of the callback, or FALSE on error.
返回回调函数的返回值,出错时返回FALSE。
由上面手册给的说明来看,call_user_func 和 call_user_func_array 函数功能基本差不多。
注意NOTE:
1. 需要注意的一点事,call_user_func()的回调函数参数不能通过引用传递,而 call_user_func_array ()是可以的。比如下面这个例子:
2. 在函数中注册有多个回调内容时(如使用 call_user_func() 与 call_user_func_array()),如在前一个回调中有未捕获的异常,其后的将不再被调用。
<?phperror_reporting(E_ALL);function increment(&$var){ $var++;}$a = 0;call_user_func('increment', $a);echo $a."\n";call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3echo $a."\n";?>上面的程序会输出:
0
1
下面我们通过例子来了解下call_user_func和call_user_func_array 具体怎么用
call_user_func() 和 call_user_func_array() 例子
<?phpfunction barber($type) //参数可以有多个{ echo "You wanted a $type haircut, no problem\n";}call_user_func('barber', "mushroom");call_user_func('barber', "shave");?>上面程序输出:
You wanted a mushroom haircut, no problemYou wanted a shave haircut, no problem
<?phpfunction barber($type,$type2) //参数可以有多个{ echo "You wanted a $type haircut, no problem $type2 \n";}call_user_func_array('barber', array("mushroom","!"));call_user_func_array('barber', array("shave","!"));?>
上面程序输出:
You wanted a mushroom haircut, no problem !You wanted a shave haircut, no problem !
call_user_func() 和 call_user_func_array() 可以调用类内部的方法
<?phpclass myclass { function say_hello($name) { echo "Hello $name"; }}$classname = "myclass";//调用类内部的函数需要使用数组方式 array(类名,方法名)call_user_func(array($classname, 'say_hello'), 'World!');call_user_func_array(array($classname, 'say_hello'), array("PHP!"));//print Hello World!//print Hello PHP!?>
call_user_func() 和 call_user_func_array() 可以用于命名空间
<?phpnamespace Foobar;class Foo { static public function test() { print "Hello world!\n"; }}call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0//print Hello world!//print Hello world!?>
call_user_func() 和 call_user_func_array() 可以用于闭包函数和 lambda 函数
<?phpcall_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 *///print [test]?>
<?php function Benchmark() { foreach(func_get_args() as $function) { $st = microtime(true); call_user_func($function); $et = microtime(true); echo sprintf("Time: %f", $et - $st) . '<br />'; } } Benchmark(function() { for ( $i = 0; $i <= 10000; $i++ ) { } }, function() { $i = 0; while ( $i <= 10000 ) { $i++; } }); ?> //print Time: 0.001652//print Time: 0.001458
http://www.php.net/manual/zh/function.call-user-func.php
http://www.php.net/manual/zh/function.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】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详解
- 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详解
- 防火墙负载均衡解决方案
- AsyncTask异步加载图片示例
- HDU 4502 吉哥系列故事——临时工计划(动态规划)
- 无法定位程序输入点……于动态链接库KERNEL32.dll上
- Scala 中的 apply 和 update 方法
- call_user_func和call_user_func_array的用法详解
- Phaser游戏框架 鼠标mouse事件以及触摸touch事件
- mysql插入int字段可以使用单引号包含?今天改东西无意发现时间字段成int类型了,而且update这个字段用单引号包含,于是在MySQL上测试下能运行,求解
- 网络棋牌游戏创造快乐心情
- 腾讯,百度,阿里的面试经验
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- Java 理论与实践: 哈希
- Android Init进程源码分析
- 北京休闲好去处 适合春游的地方