«

»

Mar 27

Hook System with PHP

I was recently coding a web-application base in PHP and it occurred to me that including a hooking system won’t be a bad idea. I like the way hooks are used in wordpress for almost every task. Implementing a hook system makes the application more flexible and modular, all with a small bit of code.

Adding modules/plugins was a cakewalk after I did away with this simple hook system:

For those wondering what the f**k is hooking, it is a technique used to add code at runtime, all dynamically as requested by various modules active on the application. They are particularly helpful as they don’t need you to edit the base code again and again, and you can add more functionality as the development proceeds, and distribute them as plugins/modules.

Actually using the hook class:

Suppose you are implementing templating of you web pages in the above snippet. Then, all the hooks hooked to the hook name “before_head_close” will be executed just before </head> is echoed to the browser. You can then later on add a plugin which does this:

To put this plugin to action, simply include the .php file in the page where you want this to work. Or you can also implement a plugin system (as I did in my app), which automatically detects the plugins present in a directory and includes them on-the-go, making your application truly modular. Just copy the module files to your modules app, and voila, the module’s already at work. Wait for my next post for the plugin system…

  • Nilesh Parate

     thanks for nice dude

  • benJamin

    thanks, I include the file hook.php in header.php,and I got this error,

    hooks=array();
    }
    function add_action($where,$callback,$priority=50)
    {
    if(!isset($this->hooks[$where]))
    {
    $this->hooks[$where]=array();
    }
    $this->hooks[$where][$callback]=$priority;
    }
    function remove_action($where,$callback)
    {
    if(isset($this->hooks[$where][$callback]))
    unset($this->hooks[$where][$callback]);
    }
    function execute($where,$args=array())
    {
    if(isset($this->hooks[$where])&&is_array($this->hooks[$where]))
    {
    arsort($this->hooks[$where]);
    foreach($this->hooks[$where] as $callback=>$priority)
    {
    call_user_func_array($callback,$args);
    }
    }
    }
    };
    $hooking_daemon=new hooking;
    function add_action($where,$callback,$priority=50)
    {
    global $hooking_daemon;
    if(isset($hooking_daemon))
    $hooking_daemon->add_action($where,$callback,$priority);
    }
    function remove_action($where,$callback)
    {
    global $hooking_daemon;
    if(isset($hooking_daemon))
    $hooking_daemon->remove_action($where,$callback);
    }
    function execute_action($where,$args=array())
    {
    global $hooking_daemon;
    if(isset($hooking_daemon))
    $hooking_daemon->execute($where,$args);
    }
    ?>

    Fatal error: Call to undefined function execute_action() in C:wampwwwr*****header.php on line 63

    Using wamp2.0, Apache Version :2.2.11 Php version : 5.2.11 / 5.3