PHP中call_user_func_array回调函数的用法示例

yipeiwu_com6年前PHP代码库

call_user_func_array

call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数

mixed call_user_func_array ( callable $callback , array $param_arr )

把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入。

例子:

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"));
dump("<br/>");
// 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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用PHP能有所帮助,如果有疑问大家可以留言交流。

相关文章

php使用post数组的键值创建同名变量并赋值的方法

本文实例讲述了php使用post数组的键值创建同名变量并赋值的方法。分享给大家供大家参考。具体如下: 这段代码可以自动根据post数组的键值创建同名变量,这个功能使用非常方便,不用提前声...

php数组函数序列之array_unique() - 去除数组中重复的元素值

array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组。 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除。 返回的...

PHP获取网卡地址的代码

复制代码 代码如下:<?php     @exec("ipconfig /all",$array);  &nb...

linux下编译安装memcached服务

linux下编译安装memcached服务

系统:Ubuntu 13.10 第一步:安装libevent-dev $aptitude search libevent-dev $aptitude install libevent...

PHP实现二维数组去重功能示例

本文实例讲述了PHP实现二维数组去重功能。分享给大家供大家参考,具体如下: php中二维数组去重操作。例如从数据库中查询出的记录,根据某个键做去重操操作 代码如下: /** * 删...