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中操作memcached缓存进行增删改查数据的实现代码

核心代码: <?php //创建一个memcache对象实例 $memcache = new Memcache; if(!$memcache->conn...

php常用日期时间函数实例小结

本文实例讲述了php常用日期时间函数。分享给大家供大家参考,具体如下: 时间戳 时间戳我就不赘述了,手册里有,就是能精确的表示一个时间点。我在做项目的时候经常用时间戳来表示数据,这样比较...

php目录拷贝实现方法

本文实例讲述了php目录拷贝实现方法。分享给大家供大家参考。具体如下: function copy_dir($src,$dst) { $dir = opendir($src);...

PHP mkdir()定义和用法

使用方法: mkdir(path,mode,recursive,context) 参数 描述 path 必需。规定要创建的目录的名称。 mode 必需。规定权限。默认是 0777。 re...

php防止网站被刷新的方法汇总

本文实例讲述了php防止网站被刷新的方法。分享给大家供大家参考。具体方法如下: 对于像采用WP建设的站来说,频繁的刷新会导致数据库吃紧。下面附上一段代码,防止频繁的刷新造成的死机情况。...