TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Hook System (http://www.talkphp.com/advanced-php-programming/1381-hook-system.html)

Nor 11-05-2007 05:10 AM

Hook System
 
I've been researching on google for some time now, I found 1 out of a thousand results for php auto updater which was useful for one of my projects, now I'm looking for a hook system for plugins and what not. Does anyone know how to make a basic PHP -> XML plugin hook system?

Karl 11-05-2007 01:08 PM

Could you be a bit more specific as to which part of this "hook" system you don't understand? Also, how does XML play a part in all this - I have a vague idea, but could you elaborate?

Tanax 11-05-2007 01:54 PM

I think he wants something like the plugin system vBulletin has, where you import a XML-file :p

Karl 11-05-2007 02:07 PM

That doesn't actually have anything to do with the hook system (well, besides it contains the hooks related to the product that the XML file represents). The XML files simply provide a way to tie togehter all the pieces of a vBulletin product, that is, the tempaltes, hooks, stylevars, etc.

The actual hook system itself has nothing to do with XML (as far as I know anyway, I may be wrong?)

Nor 11-05-2007 04:05 PM

I really don't no nothing, I want to know how the actual hook system would be set up.

Karl 11-05-2007 04:44 PM

There are many ways to do a "hook" system, one approach could be similar to this:



The whole process would work like this:
Client asks HookController to execute a hook (using getHook())
HookController asks the specified Hook (i.e. LoginHook) to execute.
After Hook execution any return value is returned to the HookController which is in turn returned to the Client.

The system could easily be extended to support plug ins and "families" of hooks - allowing you to group hooks into products.

Nor 11-05-2007 06:19 PM

Okay using your basic concept, say I got a forum software(Working on a new PHP 5 OOP one), My Hook class would be basically a get hooks, check if hook is availible(login,etc.), then execute the hook.

Code:

class HookSystem
{
        private $hooks = array();
        private $page;
        function __construct($_cpage)
        {
                $this->page = $_cpage;
        }       
        function addhook($function_name,$type,...extra parameters...)
        {
                ...update the array hooks with a new hook name
        }
        function checkhooks()
        {
                ...runs a loop, check what type of hooks where
        }
}

When running the class I could go about checking what page in viewing and then run the hooks for that page, and what type of hook it is(i.e,

registration -> form
registration -> header
registration -> captcha

)

It would ask what type of hook it is, then check if the hook is available based on something basic like my example say form, hook would be something similar to add a new field to the form with ease.

Code:

$page = htmlspecialchars($_GET['page']);
switch($page)
{
        case 'registration':
                $type = 'registration';
        break;
}
$hooks = new HookSystem($type);

function __form__($left,$right,$pos)
{       
        global $hooks;
        $hooks->addhook("__form__","registration","form",$left,$right,$pos);
}
__form__("Referrer","<input type='text' name='ref' />",3);

Just something simple like that, if you see what I'm getting at.(Most of it would be managed through MySQL database)


All times are GMT. The time now is 12:14 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0