Firefox PHP

How do we call hooks

Posted by arod 
How do we call hooks
December 08, 2009 07:41PM
in the core code, we see in many places something like so:
Language: PHP
if (isset($PHORUM["hooks"][HOOK_NAME])) { $result = phorum_hook(HOOK_NAME, [param]); }
("param" in square brackets to signal it's optional)

behind the scenes, the phorum_hook() function knows to fan the call out to as many hooks that have registered to process HOOK_NAME.

in other words, the caller does not care whether there is one, five or fifteen hooks that registered to process HOOK_NAME.
so, in a sense, it looks a little non-orthogonal that the caller *does* care whether there are zero hooks that registered.
IMO, the "if" is out of place. the call-site should not care about $PHORUM["hooks"]. it should just call
phorum_hook() unconditionally, and if there is nothing to do, phorum_hook() will do nothing.

now, if no hook is assigned, removing the condition might cost some unnecessary extra work both with computing the parameter, and in the copy-back.

1) for all call-sites where no generation of the parameter is required (or when it's very cheap) and no return value required, drop the condition.
2) create alternative "phorum_hook" function that would expect the parameter to be by ref (say, phorum_hook_byref()) or somesuch), and use it for the heavier calls
3) for all call-sites where (1) and (2) are not feasible, leave the calls as is today.

i feel that what i outlined would be an improvement in two senses:
-- the call-site code will be more streamlined
-- data hiding: the caller should not care if the hooks are stored in $PHORUM[hookname] or someplace else. in the same sense that the caller should not care whether there is one hook or ten, the caller should not care whether there is one hook or zero.

Re: How do we call hooks
December 08, 2009 08:07PM
Simple: this is Phorum. We could not care less about strict orthogonal programming in case it gets in the way of application performance.

The if statement is a lot (benchmarks tell us: 10 times, even with phorum_hook calls that have no arguments at all) cheaper than always doing the function call to phorum_hook() and letting phorum_hook() decide whether or not some module implements the called hook. Doing things this way makes sure that hooks have near zero impact on the Phorum code when no module implements the hook.

Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: How do we call hooks
December 08, 2009 09:47PM
thanks. i see.
i somehow assumed that together with more sensible objects in php5 we got more sensible function call overhead (maybe it's true when optimization is on? could not find useful data...)
i guess i was wrong then.
oh well...

Re: How do we call hooks
December 22, 2009 09:21PM
phorum_hook() will work fine as you describe it. The IF, as Maurice said, is just there for peformance. I am not sure what is true about objects or functions that lets you skip them when they return nothing. That is non-logical. Also, passing by reference is MORE overhead than by value. FYI.

Brian - Cowboy Ninja Coder - Personal Blog - Twitter
Sorry, only registered users may post in this forum.

Click here to login