Implementing hooks for modular 'add ons'
Posted: Thu Nov 11, 2004 5:32 pm
Hi All,
For my pet project, an IRC bot in PHP, I want to be able to allow addons to be made for it, and I want the addons to be able to hook into events that occur in the bot.
The basic structure I'm considering now is this:
1. A module or add on will hook into the main app something like this:
function addon(&$bot) { ... addon functionality here ... }
$hook['eventtype']=EVENT1;
$hook['functionname']='addon' ;
$bot->AddHook($hook) ;
2. $bot will keep lists of all functions that should be called for each event type. E.g. $EVENT1[]='addon' ;
3. When something occurs that could trigger a hook (e.g. a message is received), hooks will be called via arrays of event types.
e.g.
if (EVENTX has occured)
foreach ($EVENT1 as $function) {
$function($this) ;
}
Is this method of variable functions with a by-reference copy of the entire bot the best way to call the external addons?
Also wondering whether I should include the ability for the addons to define the user levels of users that they want to be called for in the $hook[] definition, or let them determine that in their own code (since they'll be able to access $bot->current_user_level or something similar) ?
Any other input on structures for implementing hooks or examples of existing code appreciated.
For my pet project, an IRC bot in PHP, I want to be able to allow addons to be made for it, and I want the addons to be able to hook into events that occur in the bot.
The basic structure I'm considering now is this:
1. A module or add on will hook into the main app something like this:
function addon(&$bot) { ... addon functionality here ... }
$hook['eventtype']=EVENT1;
$hook['functionname']='addon' ;
$bot->AddHook($hook) ;
2. $bot will keep lists of all functions that should be called for each event type. E.g. $EVENT1[]='addon' ;
3. When something occurs that could trigger a hook (e.g. a message is received), hooks will be called via arrays of event types.
e.g.
if (EVENTX has occured)
foreach ($EVENT1 as $function) {
$function($this) ;
}
Is this method of variable functions with a by-reference copy of the entire bot the best way to call the external addons?
Also wondering whether I should include the ability for the addons to define the user levels of users that they want to be called for in the $hook[] definition, or let them determine that in their own code (since they'll be able to access $bot->current_user_level or something similar) ?
Any other input on structures for implementing hooks or examples of existing code appreciated.