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 ...
随机推荐
- Linux下打开windows中文文本乱码问题
1. 查看文件的编码方式:file命令 $ file test_file.txt test_file.txt: ISO- text, with very long lines $ file train ...
- HDU 1043
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://www.cnblogs.com/goodness/archive/2010/05/04/17 ...
- JAVA多线程----用--进阶--》网络编程2
import java.io.*; import java.net.*; /** * 服务器端逻辑线程 */ public class LogicThread extends Thread { Soc ...
- CSS 文本垂直居中对齐
文本垂直居中对齐是一个很常见的问题,这里总结一下. 一.容器高度固定,单行文本垂直居中对齐 height:20px; line-height:20px; overflow:hidden; 二.容器高度 ...
- 跳转后全屏,兼容大部分浏览器JavaScript
<!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...
- THREE.OrbitControls参数控制
// Set to false to disable this control//鼠标控制是否可用 this.enabled = true; // "target" sets th ...
- AngularX Http服务总结
自己经常用的方式: 1.首先看httpModule Angular2的http访问经常采用两种方式: 共同点:导入相应的Module import {Http, RequestOptions, Res ...
- Codeforces 932E Team work 【组合计数+斯特林数】
Codeforces 932E Team work You have a team of N people. For a particular task, you can pick any non-e ...
- CentOS 6.6下安装OpenOffice4.0
最近由于项目需要,要在公司服务器上安装Openoffice,网上搜了一些资料后成功安装,现分享给大家. 1.首先先下载好需要的rpm包:Apache_OpenOffice_4.0.0_Linux_x8 ...
- 《DSP using MATLAB》示例Example 8.26
代码: %% ------------------------------------------------------------------------ %% Output Info about ...