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有序表查找之插值查找算法示例

PHP有序表查找之插值查找算法示例

本文实例讲述了PHP有序表查找之插值查找算法。分享给大家供大家参考,具体如下: 前言: 在前面我们介绍了二分查找,但是我们考虑一下,为什么一定要折半呢?而不是折四分之一或者更多? 打个比...

linux下删除7天前日志的代码(php+shell)

PHP版本: 复制代码 代码如下: /** * 删除7天前的日志 * @param $logPath */ function del7daysAgoLog($logPath) { if(...

PHP中PDO的错误处理

PHP中PDO的错误处理

面向对象的方式 先看看如果连接错误等的处理,PHP中PDO的错误处理,使用面向对象的方式来处理: 复制代码 代码如下: <?php try {  $db = new PDO('my...

解析php通过cookies获取远程网页的指定代码

复制代码 代码如下:function Steal($url, $post_data = ""){//$header[] = "Accept: text/vnd.wap.wml,*.*";...

php根据日期显示所在星座的方法

本文实例讲述了php根据日期显示所在星座的方法。分享给大家供大家参考。具体实现方法如下: <?php function zodiac($DOB){ $DOB =...