First - I am not expecting anyone to do this for me, I want to know and understand HOW to do it. I want to have plugins hooks in my CMS almost exactly the same as MyBB - except without the extra optional attributes (priority etc.).
I have considered just copying MyBB's plugin and cache class - but I would not understand how I did it, and that is crucial for me.
Still, I have absolutely no idea how to do it, so need help on what I should do.
Of course, anyone who helps will be credited fully as the creator of the plugin system in the script ACP.
Cheers,
BP
The idea of hooks is basically to execute a custom function at a certain point in time.
There are in fact many possible different ways this can be implemented.
If you want a MyBB like approach, where a plugin adds hooks, and the main script executes them, all you need to do is provide something to store the hooks (hint, an array), and something which will loop through the list and execute them.
I do like the MyBB approach...
Ok, so I am still going to be confused about how to go about it, but an array is a good idea. So lets say I'll use an array, here is what I'd have in my class:
PHP Code:
function add_hook($hook,$function){
$var = array(
hook_name => $hook,
function_name => $function
}
|
For run hooks, I do not how I'd loop through it, as both for and while loops would not work here, nor do I knw how I would execute the $var_function_name as a variable. I know there is a function for something similar, (call_user_func_array)- but I highly doubt I'd be able to use it on something like this until after a few weeks/months of practice, and I need this ASAP,
Cheers and thanks,
Josh
Umm, I'm sure you know about scope? Your array must be visible to both add hook and run hook functions.
Note that you need to
add to the array, not
assign to it.
As for running them, you can simply call them, ie:
PHP Code:
$f = 'myfunction';
$f();
myfunction();
|
Yeah I know about scope. However I just can't write out correct code in a forum post, I need to concentrate
As for adding to an array, I have no idea how to really do this at all...it may just be asier to fork out some money to have it done.
Thanks for your help, I am going to have a try tonight, and if I don't get anywhere, I'll just pay a dev
Josh
Hmmm. I have thought about doing this SQL based....with a table structure like:
Function Name|File Name|Hook_Name
Even so - executing it would still cause me no end of problems.
This is the only piece of code I have ever had absolutely no idea what the f**k I am doing. I am not used to it, and I can not stand it, and my friggin pride will not let me pay for a coder. I am seriously about to rip something out of the computer and hurl it at something.
Easy enough actually. Thanks for your help though, the base of the code:
PHP Code:
<?
class Hook{
function set_hook($hookname,$function){
$this->$hookname = $function;
}
function use_hook($hookname){
if (function_exists($this->$hookname))
{
call_user_func($this->$hookname);
}
else
{
}
}
}
?>
|
Much simple than I would have thought
That code has a number of problems, unfortunately, primarily with multiple hooks...
Instead of telling you, I'll let you figure it out.
Yeah I can figure it out by looking he code. It'll either be an "already defined function" error, or just execute the last hook.
Are arrays the only way to do this?
blueparukia Wrote:Are arrays the only way to do this?
No, but it's the most logical solution.
PHP Code:
function add_a_hook($hook, $function)
{
global $plugin_hooks;
$plugin_hooks[$hook][] = $function;
}
function run_a_hook($hook)
{
global $plugin_hooks;
foreach($plugin_hooks[$hook] as $function)
{
$function();
}
}
|
If you're really determined to do PHP scripting, you should really understand the above concept.