php call_user_func_array

call_user_func_array() 调用回掉函数,并把一个数组参数坐位回掉函数。也可以执行类中的方法。

例子:

01<?php
02function foobar($arg, $arg2) {
03    echo __FUNCTION__, " got $arg and $arg2</br>";
04}
05class foo {
06    static function bar($arg, $arg2) {
07        echo __METHOD__, " got $arg and $arg2</br>";
08    }
09 
10    function food($arg, $arg2) {
11        echo __METHOD__, " got $arg and $arg2</br>";
12    }
13}
14 
15call_user_func_array("foobar", array("one", "two"));
16 
17$foo = new foo;
18call_user_func_array(array($foo, "food"), array("three", "four"));
19//这种方式是错误的
20//call_user_func_array(array("foo", "food"), array("three", "four"));
21//下面这种办法,通过PHP的对象的方式调用一个静态方法,并不会报错。但是静态方法中不可以出现$this关键字
22call_user_func_array(array($foo, "bar"), array("three", "four"));
23//个人感觉调用静态方法还是这样会好些
24call_user_func_array(array("foo", "bar"), array("three", "four"));
25?>

结果:

1foobar got one and two
2foo::food got three and four
3foo::bar got three and four
4foo::bar got three and four

 

 

发表评论

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>