现在的位置: 首页 > 综合 > 正文

call_user_func_array 函数使用

2014年01月11日 ⁄ 综合 ⁄ 共 1019字 ⁄ 字号 评论关闭
<?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

<?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

<?php

$func = function($arg1$arg2) {
    return 
$arg1 $arg2;
};

var_dump(call_user_func_array($func, array(24))); /* As of PHP 5.3.0 */

?>

以上例程会输出:

int(8)

抱歉!评论已关闭.