如何做一个PHP钩子系统?

时间:2011-12-01 03:47:04

标签: php plugins module content-management-system hook

如何在PHP应用程序中实现钩子系统在执行之前或之后更改代码。如何将钩子类的基本体系结构用于PHP CMS(甚至是简单的应用程序)。那怎么可以扩展到一个完整的插件/模块加载器?

(另外,CMS挂钩系统上有任何书籍或教程吗?)

3 个答案:

答案 0 :(得分:30)

您可以根据需要将事件系统构建为simple或复杂。

/**
 * Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called.
 *
 * @param string $event name
 * @param mixed $value the optional value to pass to each callback
 * @param mixed $callback the method or function to call - FALSE to remove all callbacks for event
 */
function event($event, $value = NULL, $callback = NULL)
{
    static $events;

    // Adding or removing a callback?
    if($callback !== NULL)
    {
        if($callback)
        {
            $events[$event][] = $callback;
        }
        else
        {
            unset($events[$event]);
        }
    }
    elseif(isset($events[$event])) // Fire a callback
    {
        foreach($events[$event] as $function)
        {
            $value = call_user_func($function, $value);
        }
        return $value;
    }
}

添加活动

event('filter_text', NULL, function($text) { return htmlspecialchars($text); });
// add more as needed
event('filter_text', NULL, function($text) { return nl2br($text); });
// OR like this
//event('filter_text', NULL, 'nl2br');

然后像这样称呼它

$text = event('filter_text', $_POST['text']);

或者像这样删除该事件的所有回调

event('filter_text', null, false);

答案 1 :(得分:0)

这是另一种解决方案:

创建一个钩子

在您要创建钩子的任何地方运行此命令:

x_do_action('header_scripts');


向挂钩添加功能

然后通过以下操作将功能附加到上面:

x_add_action('header_scripts','my_function_attach_header_scripts');

function my_function_attach_header_scripts($values) {

   /* add my scripts here */

}

用于存储所有挂钩/事件的全局变量

将其添加到您的主要PHP函数文件或等效文件的顶部

$x_events = array();
global $x_events;

基本功能

function x_do_action($hook, $value = NULL) {
    global $x_events;

    if (isset($x_events[$hook])) {

        foreach($x_events[$hook] as $function) {

            if (function_exists($function)) { call_user_func($function, $value); }

        }
    }

}

function x_add_action($hook, $func, $val = NULL) {
    global $x_events;
    $x_events[$hook][] = $func;

}

答案 2 :(得分:0)

此解决方案存在一些问题,您无法为一个可调用的挂钩设置多个功能。 您可以放弃此代码。



 $action = [];

 function apply($hook, $args){
    global $action;
    $action[$hook]['args'] = $args;
    return doa($hook, $args);
 }

 function add($hook, $func){
    global $action;
    $action[$hook]['funcs'][] = $func;
 }

 function doa($hook,$args){
    global $action;
    if(isset($action[$hook]['funcs'])){
        foreach($action[$hook]['funcs'] as $k => $func){
            call_user_func_array($func, $args);
       }
    }
    
 }

 add('this_is', 'addOne');
function addOne($user){
    echo "this is test add one $user <br>";
}
add('this_is', function(){
    echo 'this is test add two <br>';
});


add('this_is_2', 'addTwo');
function addTwo($user, $name){
    echo $user . '   ' . $name . '<br>';
}


function test(){
    echo 'hello one <br>';
    apply('this_is', ['user'=> 123]);
}


function test2(){
    echo 'hello two <br>';
    apply('this_is_2', ['user'=> 123, 'name' => 'mohammad']);
}
test();
test2();