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流Streams、包装器wrapper概念与用法实例详解

本文实例讲述了PHP流Streams、包装器wrapper概念与用法。分享给大家供大家参考,具体如下: 流Streams这个概念是在php4.3引进的,是对流式数据的抽象,用于统一数据操...

PHP number_format() 函数定义和用法

number_format() 函数通过千位分组来格式化数字。 语法 number_format(number,decimals,decimalpoint,separator)...

php下使用SMTP发邮件的代码

最近一个项目需要用到SMTP发送邮件,之前的库类不存在了,又不喜欢安装pear或者使用pear的net/smtp类,感觉太复杂了。就直接从discuz中抽取出核心稍微修改了下。 &nbs...

写php分页时出现的Fatal error的解决方法

Fatal error: Cannot redeclare htmtocode() (previously declared in D:\www_local\mytest\conn.ph...

php版本CKEditor 4和CKFinder安装及配置方法图文教程

php版本CKEditor 4和CKFinder安装及配置方法图文教程

本文实例讲述了php版本CKEditor 4和CKFinder安装及配置方法。分享给大家供大家参考,具体如下: 下载并解压CKEditor 4和CKFinder CKEditor 4下载...