php call_user_func_array
call_user_func_array
(PHP >= 4.0., PHP , PHP )
call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
说明
mixed call_user_func_array ( callable $callback , array $param_arr )
把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入。
参数 callback
被调用的回调函数。
param_arr
要被传入回调函数的数组,这个数组得是索引数组。 返回值
返回回调函数的结果。如果出错的话就返回FALSE
更新日志
版本
说明
5.3.
对面向对象里面的关键字的解析有所增强。在此之前,使用两个冒号来连接一个类和里面的一个方法,把它作为参数来作为回调函数的话,将会发出一个E_STRICT的警告,因为这个传入的参数被视为静态方法。 范例 Example # call_user_func_array()例子
<?php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\n";
}
} // Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two")); // Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>
以上例程的输出类似于:
foobar got one and two
foo::bar got three and four
Example # call_user_func_array()使用命名空间的情况
<?php namespace Foobar; class Foo {
static public function test($name) {
print "Hello {$name}!\n";
}
} // As of PHP 5.3.0
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes')); // As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip')); ?>
以上例程的输出类似于:
Hello Hannes!
Hello Philip!
Example # 把完整的函数作为回调传入call_user_func_array()
<?php $func = function($arg1, $arg2) {
return $arg1 * $arg2;
}; var_dump(call_user_func_array($func, array(, ))); /* As of PHP 5.3.0 */ ?>
以上例程会输出:
int()
Example # 传引用
<?php function mega(&$a){
$a = ;
echo "function mega \$a=$a\n";
}
$bar = ;
call_user_func_array('mega',array(&$bar));
echo "global \$bar=$bar\n"; ?>
以上例程会输出:
function mega $a=
global $bar= 注释
Note:
PHP .4之前,如果param_arr里面的参数是引用传值,那么不管原函数默认的各个参数是不是引用传值,都会以引用方式传入到回调函数。虽然以引用传值这种方式来传递参数给回调函数,不会发出不支持的警告,但是不管怎么说,这样做还是不被支持的。并且在PHP .4里面被去掉了。而且,这也不适用于内部函数,for which the function signature is honored。如果回调函数默认设置需要接受的参数是引用传递的时候,按值传递,结果将会输出一个警告。call_user_func() 将会返回 FALSE(there is, however, an exception for passed values with reference count = , such as in literals, as these can be turned into references without ill effects — but also without writes to that value having any effect —; do not rely in this behavior, though, as the reference count is an implementation detail and the soundness of this behavior is questionable)。
php call_user_func_array的更多相关文章
- PHP函数call_user_func和call_user_func_array详解
今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的: call_user_func_array (P ...
- 简单理解call_user_func和call_user_func_array两个函数
call_user_func():调用一个回调函数处理字符串, 可以用匿名函数,可以用有名函数,可以传递类的方法, 用有名函数时,只需传函数的名称 用类的方法时,要传类的名称和方法名 传递的第一个参数 ...
- call_user_func_array使用原型
If you need to call object and class methods in PHP < 4.0.4, the following code ought to do the t ...
- php自定义函数call_user_func和call_user_func_array详解
看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函 call_user_ ...
- php中调用用户自定义函数的方法:call_user_func,call_user_func_array
看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函数,该函数允许用户调用 ...
- call_user_func_array
call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 参数 callback 被调用的回调函数. param_arr 要被传入回调函数的数组,这个数组得是索引 ...
- (转载)php之call_user_func_array的简易用法
(转载)http://www.cnitblog.com/neatstudio/archive/2006/07/21/13990.html php之call_user_func_array的简易用法 今 ...
- php call_user_func和call_user_func_array
首先要看这个页面关于callable类型:http://www.php.net/manual/zh/language.types.callable.php 自 PHP 5.4 起可用 callable ...
- 无论url请求什么.都可以拼接class类名.实例化.传递get参数-->给当前控制器-->传递给抽象父类-->都交给抽象父类.这个方法去处理call_user_func_array()
<?phpdefine('DS','/');define('A_PATH',str_replace('\\','/',dirname(__FILE__)).DS); //01获取到主程序目录cl ...
- A框架 第二部 实例化接收到的get类,调用父类抽象方法,自动执行方法call_user_func_array()
01父类抽象类 abstract.php <?phpabstract class controller_abstract{ protected $app; function __construc ...
随机推荐
- 基于sklearn进行文本向量化
sklearn中,计数向量化用CountVectorizer,tfidf向量化用TfidfVectorizer: import pickle from sklearn.feature_extracti ...
- 369C Valera and Elections
http://codeforces.com/problemset/problem/369/C 树的遍历,dfs搜一下,从根节点搜到每个分叉末尾,记录一下路况,如果有需要修复的,就把分叉末尾的节点加入答 ...
- 在FP与DDD的道路上越走越远
托辞 最近一口气读了若干本FP方面的书,可人是越来越懒了,要整理个什么东西却也没有那个精力,所以简单扔几张图,算是给自己一个提醒吧. 在此期间,我用并不纯熟的Scala和Groovy练了一把手.虽然只 ...
- restful 协议 +面试
restful 协议:面向资源软件架构风格 API 定义 一些预先定义的函数,目的是能够让应用程序或开发人员能够具有访问指定网络资源的能力,而无需关心访问的远吗以及内部的工作机制细节. RESTful ...
- HDU2181 哈密顿绕行世界问题
解题思路:哈密顿环游世界问题.一道简单的题目,用回溯. #include<cstdio> #include<cstring> #include<algorithm> ...
- javascript的slice()与splice()方法
(1)数组和String对象都有slice()方法. //Array var list = ['A','B','C','D','DS']; console.log(list.slice(,));//截 ...
- java区分绝对路径和相对路径
java区分绝对路径和相对路径 这里要区分的是目录路径 如: /opt/deve/tomcat/bin c:\deve\tomcat\bin 都是绝对目录路径 bin bin/data bin\dat ...
- 《DSP using MATLAB》示例Example 8.6
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 借助CustomBehaviorsLibrary.dll写出水印效果(转)
在项目中载入这个dll 之后引用 使用方法具体如下图: 在这里需要注意到是项目中对interactivity的引用 : 好文要顶 关注我 收藏该文
- CentOS 6.8 源码安装mysql 5.6
一:卸载旧版本 rpm -qa | grep mysql rpm -e mysql #普通删除模式 rpm -e --nodeps xxx(xxx为刚才的显示的列表) # 强力删除模式,如果使用上面命 ...