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

相关文章

php实现求相对时间函数

本文实例讲述了php实现求相对时间函数。分享给大家供大家参考。具体实现方法如下: <?php function relativeTime($time = false,...

php中字符查找函数strpos、strrchr与strpbrk用法

本文实例讲述了php中字符查找函数strpos、strrchr与strpbrk用法。分享给大家供大家参考。具体如下: ① strpos() 函数返回字符串在另一个字符串中第一次出现的位置...

PHP编程快速实现数组去重的方法详解

本文实例讲述了PHP编程快速实现数组去重的方法。分享给大家供大家参考,具体如下: 概述 使用PHP的array_unique()函数允许你传递一个数组,然后移除重复的值,返回一个拥有唯一...

PHP调用Linux命令权限不足问题解决方法

业务背景:  yourcmd为我的linux程序,它对权限要求非常严格,当用php去执行yourcmd程序 系统:CentOS 6.3 apache是php的执行用户 用exe...

PHP的面试题集,附我的答案和分析(一)

面试题1 1、用PHP打印出前一天的时间格式是2006-5-10 22:21:21 2、echo(),print(),print_r()的区别 3、能够使HTML和PHP分离开...