Registering hooks
Now to actually defining your functions which should be called. For this we take the append_sid() function as an example (this function is able to be hooked by default). We create two classes, one being static and a function:
class my_append_sid_class
{
// Our functions
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
// Get possible previous results
$result = $hook->previous_hook_result('append_sid');
return $result['result'] . '<br />And i was the second one.';
}
}
// Yet another class :o
class my_second_append_sid_class
{
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
// Get possible previous results
$result = $hook->previous_hook_result('append_sid');
echo $result['result'] . '<br />I was called as the third one.';
}
}
// And a normal function
function my_append_sid(&$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
// Get possible previous results
$result = $hook->previous_hook_result('append_sid');
return 'I was called as the first one';
}
// Initializing the second class
$my_second_append_sid_class = new my_second_append_sid_class();
Make sure you add the same parameters to your function as is defined for the hookable function with one exception: The first variable is always &$hook... this is the hook object itself you are able to operate on.
Now we register the hooks one by one with the $phpbb_hook->register() method:
// Now, we register our append_sid "replacements" in a stacked way...
// Registering the function (this is called first)
$phpbb_hook->register('append_sid', 'my_append_sid');
// Registering the first class
$phpbb_hook->register('append_sid', array('my_append_sid_class', 'my_append_sid'));
$phpbb_hook->register('append_sid', array(&$my_second_append_sid_class, 'my_append_sid'));
With this you are even able to make your own functions that are already hooked itself being hooked again...
// Registering hook, which will be called
$phpbb_hook->register('append_sid', 'my_own_append_sid');
// Add hook to our called hook function
$phpbb_hook->add_hook('my_own_append_sid');
// Register added hook
$phpbb_hook->register('my_own_append_sid', 'also_my_own_append_sid');
Special treatment/chains
The register method is able to take a third argument to specify a special 'chain' mode. The valid modes are first, last and standalone
$phpbb_hook->register('append_sid', 'my_own_append_sid', 'first') would make sure that the function is called in the beginning of the chain. It is possible that more than one function is called within the first block - here the FIFO principle is used.
$phpbb_hook->register('append_sid', 'my_own_append_sid', 'last') would make sure that the function is called at the very end of the chain. It is possible that more than one function is called within the last block - here the FIFO principle is used.
$phpbb_hook->register('append_sid', 'my_own_append_sid', 'standalone') makes sure only the defined function is called. All other functions are removed from the chain and no other functions are added to it later on. If two applications try to trigger the standalone mode a PHP notice will be printed and the second function being discarded.
Only allowing hooks for some objects
Because the hook system is not able to differate between initialized objects and only operate on the class, you need to solve this on the code level.
One possibility would be to use a property:
class my_hookable_object
{
function blabla()
{
}
}
class my_hookable_object2 extends my_hookable_object
{
var $call_hook = true;
function hook_me($my_first_parameter, $my_second_parameter)
{
if ($this->call_hook)
{
global $phpbb_hook;
if ($phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
{
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
{
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
}
}
}
return 'not hooked';
}
}
function hooking(&$hook, $first, $second)
{
return 'hooked';
}
$first_object = new my_hookable_object2();
$second_object = new my_hookable_object2();
$phpbb_hook->add_hook(array('my_hookable_object2', 'hook_me'));
$phpbb_hook->register(array('my_hookable_object2', 'hook_me'), 'hooking');
// Do not call the hook for $first_object
$first_object->call_hook = false;
echo $first_object->hook_me('first', 'second') . '<br />';
echo $second_object->hook_me('first', 'second') . '<br />';
OUTPUT:
A different possibility would be using a function variable (which could be left out on passing the function variables to the hook):
class my_hookable_object
{
function blabla()
{
}
}
class my_hookable_object2 extends my_hookable_object
{
function hook_me($my_first_parameter, $my_second_parameter, $hook_me = true)
{
if ($hook_me)
{
global $phpbb_hook;
if ($phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
{
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
{
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
}
}
}
return 'not hooked';
}
}
function hooking(&$hook, $first, $second)
{
return 'hooked';
}
$first_object = new my_hookable_object2();
$second_object = new my_hookable_object2();
$phpbb_hook->add_hook(array('my_hookable_object2', 'hook_me'));
$phpbb_hook->register(array('my_hookable_object2', 'hook_me'), 'hooking');
echo $first_object->hook_me('first', 'second', false) . '<br />';
echo $second_object->hook_me('first', 'second') . '<br />';
OUTPUT: