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. oracle数据库导入导出09192255

    1.导入数据库(dmp) Linux系统中: 1. 用root账号登录服务器,然后切换到oracle账号(安装oracle数据库的时候用的用的账号) 2. 切换到oralce的安装目录下的bin下 3 ...

  2. java中的Reference

    这两天又重新学习了一下Reference,根据网上的资源做了汇总. Java中的引用主要有4种: 强引用 StrongReference: Object obj = new Object(); obj ...

  3. Ubuntu Kylin14.04下PHP环境的搭建(LAMP)

    1.首先打开命令行,切换到root身份,获得最新的软件包 su root sudo apt-get install update 2.安装MySQL数据库 sudo apt-get install m ...

  4. onerror="javascript:this.src='images/defaultUpload.png';"【容易导致死循环报错】

    当无法找到默认图片时,onerror="javascript:this.src='images/defaultUpload.png';"容易导致死循环报错

  5. Maven系列(二)之安装和配置详解

    检查JDK环境 在安装Maven之前,首先要确认你已经正确安装了JDK.Maven可以运行在JDK 1.4及以上的版本上. 打开cmd输入: java -version 下载Maven Maven官网 ...

  6. 简单实现Jmail发送邮件

    package com.chauvet.util; import java.util.Properties; import javax.mail.*; import javax.mail.intern ...

  7. 剑指offer第四章

    剑指offer第四章 1.二叉树的镜像 二叉树的镜像:输入一个二叉树,输出它的镜像 分析:求树的镜像过程其实就是在遍历树的同时,交换非叶结点的左右子结点. 求镜像的过程:先前序遍历这棵树的每个结点,如 ...

  8. 《DSP using MATLAB》示例Example 8.18

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. h5移动端flexible源码适配终端解读以及常用sass函数

    ;(function(win, lib) { var doc = win.document;// win = window,lib = window.lib; var docEl = doc.docu ...

  10. IIS6自带FTP安装及配置方法

    参考:http://v.huweishen.com/video/9.html ·IIS自带的FTP服务是最安全的,不会像Serv-U那样存在各种漏洞:但其配置过程也比较复杂. ·本节将以虚拟目录方式, ...