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:

 

<?

/*

* Author: technofreak777@gmail.com

*/

class hooking{

private $hooks;

function __construct()

{

$this->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);

}

?>

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:

<head>

<?php execute_action("after_head_open"); ?>

<title><?php echo $this->title; ?></title>

<?php execute_action("before_head_close"); ?>

</head>   <body>

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:

 

<?php

add_action("before_head_close","add_analytics_code");

function add_analytic_code()

{

echo "<script> var... </script>"; //The analytics code

}

?>

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…

 

http://thetechnofreak.com/technofreak/hook-system-php/

Leave a comment