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中将html中的br换行符转换为文本输入中的换行符

下面这几个方法将能够帮你解决这个问题。 PHP版将html中的<br />换行符转换为文本框中的换行符: 复制代码 代码如下:function br2nl($text){&n...

php学习之数据类型之间的转换介绍

复制代码 代码如下: /*数据类型之间相互转换 * 一种是强制转换 * setType(变量,类型); //int,integer,float,double等等。 * 这个函数将原变量的...

PHP时间类完整实例(非常实用)

本文实例讲述了PHP时间类。分享给大家供大家参考,具体如下: <?php header("Content-type:text/html;Charset=utf-8");...

php传值赋值和传地址赋值用法实例分析

本文实例讲述了php传值赋值和传地址赋值用法。分享给大家供大家参考。具体如下: <?php $name = 'Simon'; //对变量$name进行赋值(传值赋值)...

PHP操作xml代码

复制代码 代码如下:/*获取图片新闻列表*/ function getPicNews($number=5){ require_once 'include/content_class.ph...