duanji2014 2013-12-15 13:32
浏览 19
已采纳

PHP如何创建简单的钩子?

I already tried to Google how to do this, but I still can't figure it out. How do I create a simple hook inside PHP? Here is what I've tried so far:

wrapper.php (application starts here)

<?php   require 'core.php';   ?>
<?php   require 'view.php';   ?>

core.php

<?php

$hooks['hook_head'] = array();

//supposed to insert action in $hooks
function addHook($hookName, $funct){
    global $hooks;
    array_push($hooks[$hookName], $funct);
}


//supposed to execute specific hook
function executeHook($hookname){
    global $hooks;
    foreach($hooks[$hookname] as $funct){
        call_user_func( $funct );
    }
}

//supposed to execute action in $hooks when array key = hook_head
function hook_head(){
    global $hooks;
    if (array_key_exists('hook_head', $hooks)) {
        executeHook( 'hook_head' );
    }
}

//supposed to execute action in $hooks when array key == hook_footer
function hook_footer(){
    global $hooks;
    if (array_key_exists('hook_footer', $hooks)) {
        executeHook( 'hook_footer' );
    }
}

?>

view.php

<!DOCTYPE HTML>
<html>
<head>
    <?php   hook_head();  ?>
</head>
<body>
    <hr />

    <?php
        //add action to hook start here
        $test = function(){    echo "test succeds";    };
        addHook('hook_head', $test);
    ?>

    <?php  hook_footer();  ?>
</body>
</html>

The code works fine if I insert an action inside $hooks['hook_head'], before I call hook_head(). What I actually am trying to do though, is insert the action after I called hook_head(). How do I do this?

  • 写回答

1条回答 默认 最新

  • dslkchyv673627 2013-12-15 13:37
    关注

    Move the addHook call to before the hook_head call (preferably to the beginning of the file).

    I did notice that you "want to start coding (insert action in hook head) after it call hook_head()", but that's very cumbersome. It would look something like that:

    • Change hook_head() to insert some kind of token into the output.
    • At the end of the script,
      1. call ob_get_clean() to get the output buffer of the page.
      2. For each hook:
        1. Execute the hook.
        2. Call ob_get_clean() again to get the output buffer of the hook.
        3. Replace the appropriate token in the oputput buffer of the page with the output buffer of the hook.
      3. Print the output buffer of the page.

    As I said: cumbersome. I strongly advice against it. I only mentioned it because you asked specifically. It contradicts the intuitive control flow of

    1. Input
    2. Processing
    3. Output
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么