我可以制作一系列功能吗?

时间:2014-03-09 20:45:18

标签: php arrays function hook

我可以创建一个函数数组并使用foreach循环执行函数吗?

像:

// This is the beginning of the php file
global $actions = array();

// This is the function where functions need to be added.
function HeadFunction(){
 foreach($actions as $action) 
 $action;
}

这是添加动作的功能

function add_action($action){
global $actions;
$actions[] = $action;
}

这是一个示例函数:

function check_Url(){
 if(isset($_GET['gettest'])){
    echo '<script>alert(\'This is a test\');</script>';
 }
}

然后添加函数:add_action(check_Url());

1 个答案:

答案 0 :(得分:0)

使用call_user_func

global $actions = array();

function add_action($action){
global $actions;
$actions[] = $action;
}

add_action('check_Url');
add_action('function2');

foreach ($actions AS $action) {
    call_user_func($action);
}