微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php自定义函数call_user_func和call_user_func_array详解

call_user_func函数类似于一种特别的调用函数方法,使用方法如下:
<div class="codetitle"><a style="CURSOR: pointer" data="47938" class="copybut" id="copybut47938" onclick="doCopy('code47938')"> 代码如下:

<div class="codebody" id="code47938">
function a($b,$c)
{
echo $b;
echo $c;
}
call_user_func('a',"111","222");
call_user_func('a',"333","444");
//显示 111 222 333 444
?>

调用类内部的方法比较奇怪,居然用的是array,不知道开发者是如何考虑的,当然省去了new,也是满有新意的:
<div class="codetitle"><a style="CURSOR: pointer" data="39652" class="copybut" id="copybut39652" onclick="doCopy('code39652')"> 代码如下:
<div class="codebody" id="code39652">
class a {
function b($c)
{
echo $c;
}
}
call_user_func(array("a","b"),"111");
//显示 111
?>

call_user_func_array函数和call_user_func很相似,只不过是换了一种方式传递了参数,让参数的结构更清晰:
<div class="codetitle"><a style="CURSOR: pointer" data="17625" class="copybut" id="copybut17625" onclick="doCopy('code17625')"> 代码如下:
<div class="codebody" id="code17625">
function a($b,$c)
{
echo $b;
echo $c;
}
call_user_func_array('a',array("111","222"));
//显示 111 222
?>

call_user_func_array函数也可以调用类内部的方法
<div class="codetitle"><a style="CURSOR: pointer" data="57146" class="copybut" id="copybut57146" onclick="doCopy('code57146')"> 代码如下:
<div class="codebody" id="code57146">
Class ClassA
{
function bc($b,$c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'),"222"));
//显示 333
?>

call_user_func函数和call_user_func_array函数支持引用,这让他们和普通的函数调用更趋于功能一致:
<div class="codetitle"><a style="CURSOR: pointer" data="38584" class="copybut" id="copybut38584" onclick="doCopy('code38584')"> 代码如下:<div class="codebody" id="code38584">
function a(&$b)
{
$b++;
}
$c = 0;
call_user_func('a',&$c);
echo $c;//显示 1
call_user_func_array('a',array(&$c));
echo $c;//显示 2

PHP之call_user_func_array的简易用法
今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的:
call_user_func_array
(PHP 4 >= 4.0.4,PHP 5)
call_user_func_array -- Call a user function given with an array of parametersDescription
mixed call_user_func_array ( callback function,array param_arr )
Call a user defined function given by function,with the parameters in param_arr.
然后还有一个例子:
<div class="codetitle"><a style="CURSOR: pointer" data="20830" class="copybut" id="copybut20830" onclick="doCopy('code20830')"> 代码如下:<div class="codebody" id="code20830">
<?PHP
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"));
// 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
Example #2 call_user_func_array() using namespace name
<div class="codetitle"><a style="CURSOR: pointer" data="47533" class="copybut" id="copybut47533" onclick="doCopy('code47533')"> 代码如下:<div class="codebody" id="code47533">
<?PHP
namespace Foobar;
class Foo {
static public function test($name) {
print "Hello {$name}!\n";
}
}
// As of PHP 5.3.0
call_user_func_array(NAMESPACE .'\Foo::test',array('Hannes'));
// As of PHP 5.3.0
call_user_func_array(array(NAMESPACE .'\Foo','test'),array('Philip'));
?>

以上例程的输出类似于:
Hello Hannes!
Hello Philip!
Example #3 Using lambda function
<div class="codetitle"><a style="CURSOR: pointer" data="67423" class="copybut" id="copybut67423" onclick="doCopy('code67423')"> 代码如下:<div class="codebody" id="code67423">
<?PHP
$func = function($arg1,$arg2) {
return $arg1 $arg2;
};
var_dump(call_user_func_array($func,array(2,4))); /
As of PHP 5.3.0 */
?>

以上例程会输出
int(8)
相信看了例子之后应该有点明白了吧?
我自己是这么理解这个函数的,如果说的不对,还望各位高手不要耻笑:
函数真正的用法有点类似于函数重载,因为他的第一个参数是字符型的,也就是函数名称,第二个参数是数组,我们可以当成该函数的各个参数,而事实上也就是这么用的,如果你看过我的前一篇文章PHP的伪重载 ,或许你能够理解,正是因为这个函数的存在,我发现函数重载也可以这样运用:
<div class="codetitle"><a style="CURSOR: pointer" data="34311" class="copybut" id="copybut34311" onclick="doCopy('code34311')"> 代码如下:<div class="codebody" id="code34311">
/*
例子写完后,本来认为完事了,结果遇到有人问call_user_func_array(),看了一下手册
原来,我上面的那个test函数还可以精简成如下的例子,
/
function otest1 ($a)
{
echo( '一个参数' );
}
function otest2 ( $a,$b)
{
echo( '二个参数' );
}
function otest3 ( $a,$b,$c)
{
echo( '三个啦' );
}
function otest ()
{
$args = func_get_args();
$num = func_num_args();
call_user_func_array( 'otest'.$num,$args );
}
otest(1,2);

看到不?而我最初的写法,在PHP的伪重载一文中有所提及,仅作参考。。。。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐