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的更多相关文章

  1. PHP函数call_user_func和call_user_func_array详解

    今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的: call_user_func_array (P ...

  2. 简单理解call_user_func和call_user_func_array两个函数

    call_user_func():调用一个回调函数处理字符串, 可以用匿名函数,可以用有名函数,可以传递类的方法, 用有名函数时,只需传函数的名称 用类的方法时,要传类的名称和方法名 传递的第一个参数 ...

  3. 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 ...

  4. php自定义函数call_user_func和call_user_func_array详解

    看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函 call_user_ ...

  5. php中调用用户自定义函数的方法:call_user_func,call_user_func_array

    看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函数,该函数允许用户调用 ...

  6. call_user_func_array

    call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 参数 callback 被调用的回调函数. param_arr 要被传入回调函数的数组,这个数组得是索引 ...

  7. (转载)php之call_user_func_array的简易用法

    (转载)http://www.cnitblog.com/neatstudio/archive/2006/07/21/13990.html php之call_user_func_array的简易用法 今 ...

  8. php call_user_func和call_user_func_array

    首先要看这个页面关于callable类型:http://www.php.net/manual/zh/language.types.callable.php 自 PHP 5.4 起可用 callable ...

  9. 无论url请求什么.都可以拼接class类名.实例化.传递get参数-->给当前控制器-->传递给抽象父类-->都交给抽象父类.这个方法去处理call_user_func_array()

    <?phpdefine('DS','/');define('A_PATH',str_replace('\\','/',dirname(__FILE__)).DS); //01获取到主程序目录cl ...

  10. A框架 第二部 实例化接收到的get类,调用父类抽象方法,自动执行方法call_user_func_array()

    01父类抽象类 abstract.php <?phpabstract class controller_abstract{ protected $app; function __construc ...

随机推荐

  1. 【剑指offer】不使用新变量,交换两个变量的值,C++实现

    # 题目 不使用新变量,交换两个变量的值. # 思路 方法一:使用加减法操作,交换两个变量的值. A = A+B B = A-B A = A-B 方法二:使用异或运算,交换两个变量的值 A = A^B ...

  2. c# mysql and sqlserver数据库连接字符串

    .net 项目访问sqlserver 和mysql 两种数据库时,连接字符串有些不一样 具体配置如下 <connectionStrings> <add name="mysq ...

  3. Form表单如何传递List数组对象到后台的解决办法(转)

    举例说明: [后台有一个对象 User    一个PhotoDo对象],结构如下: public class User{ private String username; private List&l ...

  4. IntelliJ-IDEA中mybatis三剑客

    一.mybatis-generator的使用 作用:根据数据库自动生成pojo.dao和xml文件. 1.引入mybatis-generator pom.xml中引入配置:

  5. 解决遇到Linux网络配置,从熟悉网络配置文件入手

    如果接触过Linux,网络配置是一个比较棘手的问题.但是Linux是文件为基础来构建的系统,包括我们windows中设备,Linux也视为文件.所以只要我们明白文件的作用.就能对Linux更加的熟悉, ...

  6. nginx 调试

    配置单进程非daemon方式启动 daemon off; master_process off;

  7. 操作系统CPU上下文切换

    关于CPU,有3个重要的概念:上下文切换(context switchs),运行队列(Run queue)和使用率(utilization). 上下文切换: 目前流行的CPU在同一时间内只能运行一个线 ...

  8. 使用 Excel 可以很方便的做程序原型

    使用 Excel 可以很方便的做程序原型 比如计算 单片机的端口模式,可以使用 Excel 很方便的计算出来,花了 15 分钟做好. 还可以使用函数自动根据二进制计算出 十六进制. 然后如果再使用软件 ...

  9. 从汇编的角度看待变量类型与sizeof的机制

    1.动机:前段时间,一直有个疑问,就是编译器是从哪里知道数据的类型的,数据的类型是存在内存里面的么,因为自己调试编译器,发现内存中并没有多余的数据,后来在群上发问,才知道数据在编译成汇编的过程就知道数 ...

  10. Hibernate学习3—映射对象标识符(OID)

    一.Hibernate 用对象标识符(OID)来区分对象 作如下代码的实验: public class StudentTest { public static void main(String[] a ...