call_user_func_array is a useful function to call a callback with an array of parameters. The callback could be user-defined function or a method in a class. Here is the summary of its application.




Syntax

mixed call_user_func_array ( callable $callback , array $param_arr )

call_user_func_array call function

function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2";
}
call_user_func_array("foobar", array("one", "two"));

call_user_func_array call Class method

class foo {
    function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2";
    }
}

$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));

call_user_func_array call static method using namespace

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'));
//or
// As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));

call_user_func_array call public method using namespace

namespace Foobar;

class Foo {
    public function test($name) {
        print "Hello {$name}!\n";
    }
}

// As of PHP 5.3.0

call_user_func_array(new __NAMESPACE__ .'\Foo\test', array('Hannes'));
//or
// As of PHP 5.3.0
call_user_func_array(array(new __NAMESPACE__ .'\Foo', 'test'), array('Philip'));