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. [Python] dict字典排序和多条件排序

    利用lambda实现排序:要实现多条件排序,只需要依次指定排序的标准,具体实现如下 counter = {'是': 1, '不是': 1, '你': 3} counter_list = sorted( ...

  2. 04-python第四天学习

    (1)for循环里的else In [1]: nums = [11,22,33,44] In [2]: for temp in nums: ...: print(temp) ...: else: #e ...

  3. ubuntu创建Centos7镜像&&配置运行环境

    1. 下载centos7镜像 sudo docker pull centos:7 2. 启动centos7容器并挂载本地目录 sudo docker -it -v /home/software:/ho ...

  4. 【C++】STL之队列queue

    1.头文件 # include<queue> 2.成员函数 empty() 当队列为空时,返回true size() 返回队列内元素个数 front() 返回队首元素 back() 返回队 ...

  5. kubeadm搭建kubernetes集群之三:加入node节点

    在上一章<kubeadm搭建kubernetes集群之二:创建master节点>的实战中,我们把kubernetes的master节点搭建好了,本章我们将加入node节点,使得整个环境可以 ...

  6. php simple_html_dom

    这个真的很好用,如果用正则,就太麻烦了. 首先,下载simple_html_dom,用include_once就可以使用了. 可以直接定位,可以像个对象一样操作,很方便. $ret=file_get_ ...

  7. BZOJ4987:Tree (树形DP)

    Description 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一行两个正整数n,k,表示数的顶点数和需要 ...

  8. BZOJ2555 SubString【后缀自动机+LCT】

    Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...

  9. BZOJ1260 CQOI2007 涂色paint 【区间DP】

    BZOJ1260 CQOI2007 涂色paint Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字 ...

  10. HDU5126 stars【CDQ分治】*

    HDU5126 stars Problem Description John loves to see the sky. A day has Q times. Each time John will ...