2010-03-23 79 views
0

现在,我开始学习如何处理PHP函数作为参数,现在我有这样的功能。多个参数不知道理论总数? (PHP函数作为参数主题)

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

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

    extract($args, 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); 

如何使这段代码能够返回并处理多个参数而不知道所有参数的总数?如你所知,日期能够处理2个参数。

日期(字符串$格式[摘要$时间戳])

,我想拓宽do_action到能够在没有总参数的极限工作。 (请参阅顶部代码中的HERE),它可以将每个函数作为变量返回。

回答

2

我觉得你要找的是call_user_func_array()

编辑:喜欢的东西:

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

非常感谢你......你帮了我很多。我会检查你给我的函数的参考; D – justjoe 2010-03-23 09:42:00