MyBB Community Forums

Full Version: Making a distinction between hooks?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone. I'm currently devoloping my own plugin, and I was wondering if it's possible to make an actual distinction between two hooks which use the same function. Let me explain that. Let's say I have this:

$plugins->add_hook("newreply_do_newreply_end", "someFunc");
$plugins->add_hook("newthread_do_newthread_end", "someFunc");

function someFunc()
{
    //...
}

Is there a way to tell which hooks is calling/using someFunc()? I think it would be a waste of lines making a new function to only change one single thing. I thought using some parameter in someFunc() like someFunc($hook) might work, but then realised I cannot use parameters when adding a hook...

I'd appreciate your help. Big Grin
You could check whether a specific variable is set.
Or (in this case) you could check "THIS_SCRIPT".
It depends on what hooks you are using. The ones you have posted can be distinguished by the $new_thread variable for example, which is defined only in newthread.php and not in newreply.php. You'd just check for it's definition to know whether if the code is running on one hook or on the another one:

if ($new_thread) {
    // newthread_do_newthread_end
}
else {
    // newreply_do_newreply_end
}

There might be other vars that are defined in one of the two files which can be checked with this flow, but this is probably the one I'd use if I were you.
---
Edit: ninj'd Toungue
$plugins->current_hook

but in your example, checking THIS_SCRIPT is probably the better option
Thanks for the quick reply to the three of you! It works like charm.

(2014-02-11, 09:37 PM)frostschutz Wrote: [ -> ]$plugins->current_hook

I didn't know that was possible. I took a look at class_plugins.php and only found:

add_hook
run_hooks
run_hooks_by_ref
remove_hook
is_compatible
(2014-02-12, 09:35 AM)daakurai Wrote: [ -> ]
(2014-02-11, 09:37 PM)frostschutz Wrote: [ -> ]$plugins->current_hook

I didn't know that was possible. I took a look at class_plugins.php and only found:

add_hook
run_hooks
run_hooks_by_ref
remove_hook
is_compatible

Those are functions. current_hook is a class variable.