PHP Function handling functions

1.call_user_func_array

mixed call_user_func_array(callable $callback, array $param_arr)

调用callback函数并把数组param_arr中的元素作为参数传给callback函数。

参数说明:

callback---被调用的函数

param_arr---数组,包含的元素将作为参数传给callback.

返回值:返回callback函数的返回值,如果出错返回FALSE.

 

例一:

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

 

例二:

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!



 

你可能感兴趣的:(PHP)