开发者

multiple parameters without knowing the argumen total ? (PHP functions as parameter topic)

Right now, i start to learn how to process PHP function as parameter and now i have function like this.

    function do_action($func_name) {
    if (func_num_args() <= 1) {
        return $func_name();
    } 

    $args = func_get_args();
    unset ($args[0]);        

    extract($arg开发者_C百科s, EXTR_PREFIX_ALL , "foo");        
    return $func_name($foo_1, $foo_2);  //HERE ;)
}

echo do_action('time');
echo do_action('date', 'D, d F Y', 1267515462);  

How to make this snippet of code has the ability to return and process multiple parameters without knowing the total of the argumen ? As you know it, date capable to process 2 parameter.

date ( string $format [, int $timestamp ] )

And i want to broaden do_action to able to work without limit of total parameter. (See //HERE on the top code), it able to return every function as variable.


I think what you're looking for is call_user_func_array().

Edit: Something like:

function do_action($func) {
    $args = func_get_args();
    array_shift($args); // removes the first element
    return call_user_func_array($func, $args); // magic!
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜