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

yipeiwu_com5年前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能有所帮助,如果有疑问大家可以留言交流。

相关文章

真正的ZIP文件操作类(php)

<? /******************** 作者未知 整理: Longbill ( www.longbill.cn ; lo...

利用curl 多线程 模拟 并发的详解

首先,先了解下 php中的curl多线程函数:复制代码 代码如下:# curl_multi_add_handle# curl_multi_close# curl_multi_exec#...

ini_set的用法介绍

PHP ini_set用来设置php.ini的值,在函数执行的时候生效,脚本结束后,设置失效。无需打开php.ini文件,就能修改配置,对于虚拟空间来说,很方便。 函数格式:string...

php表单提交与$_POST实例分析

php表单提交与$_POST实例分析

本文实例分析了php的表单提交与$_POST。分享给大家供大家参考。具体如下: 这里要注意:表单 checkbox 的 name 需要以数组形式来命名。 表单文件:index.php 复...

解析PHP多种序列化与反序列化的方法

序列化是将变量转换为可保存或传输的字符串的过程;反序列化就是在适当的时候把这个字符串再转化成原来的变量使用。这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性。1. seri...