Page 1 of 1

PHP "hooks"?

Posted: Mon Nov 27, 2006 5:55 am
by Mr Tech
I'm not sure if hooks is the word for it... However, I noticed with vBulletin and WordPress, they use something that I think is called "hooks" to include code which is added by third party plugins. Correct me if I'm wrong.

The Plugin specifies what section of a certain page they want to add the code to and the hook includes that PHP into that location.

Here is a sample from vBulletin:

Code: Select all

$hook_query_fields = $hook_query_joins = '';
($hook = vBulletinHook::fetch_hook('showpost_start')) ? eval($hook) : false;
Hiopefully I'm making sense. I did a search for "php hooks" in google but found nothing. Perhaps you can shed some light on the correct name and even tutorials if you have any...


Thanks

Posted: Mon Nov 27, 2006 6:52 am
by Jenk
Without seeing the code recently for vB, it would appear to be an implementation of the Observer pattern, which updates on each section of the page, in a flow of the model relaying messages to the observer with such messages as (in paraphased pseudo) "I'm making the header now, any plugins for the header?" and the plugins responding when appropriate, and being included when necessary.

For a quick and dirty example, without class def's (assume that Plugin eval's it content):

Code: Select all

<?php

$observer = new Observer;
$observer->register(new Plugin('header', 'echo "Foo";'));
$observer->register(new Plugin('main', 'echo "Man";'));
$observer->register(new Plugin('footer', 'echo "Shoe!";');


foreach (array('header', 'main', 'footer') as $section)
{
    $observer->notify($section);
}

/**
* result:
* 'FooManShoe!'
*/

?>

Posted: Mon Nov 27, 2006 8:22 am
by theFool
As far as I know hooks are used with the template methode pattern (or sometimes easily called Template Pattern). Hooks are called within that template pattern to delegate functionality at certain points in the process chain to subclasses.

One link I found was at wikipedia: http://en.wikipedia.org/wiki/Template_method_pattern

Might help.

Posted: Mon Nov 27, 2006 2:24 pm
by Mr Tech
Shame that wkipedia has no php example. Atleast I've got something to work with now.

I found the class code for the hook file. I can't share the code due to their copyright but I'll have a look through it and see what I can find out. It reads an xml file to determine all the different areas you can "hook" your code.

Posted: Wed Dec 13, 2006 2:07 pm
by Begby
A really good book for this kind of thing is "Head First: Design Patterns" by Oreilly Press. Its written for Java, but damn, if you want to learn this kind of stuff go get this book right now.

Posted: Wed Dec 13, 2006 7:26 pm
by Ollie Saunders
I second I saw 'hook' I thought of HFDP. I finished reading that book about 2 or 3 months ago and whilst I'm not completely sure about the heads first style, I think my dyslexia responds badly to the exercises, the sheer comprehensiveness and focus on explaining the principles behind the patterns just cannot be bettered. I felt like my potential salary just went up about 7K.

Posted: Wed Dec 13, 2006 8:15 pm
by Begby
ole wrote:I second I saw 'hook' I thought of HFDP. I finished reading that book about 2 or 3 months ago and whilst I'm not completely sure about the heads first style, I think my dyslexia responds badly to the exercises, the sheer comprehensiveness and focus on explaining the principles behind the patterns just cannot be bettered. I felt like my potential salary just went up about 7K.
You should check out the new head first OO analysis and desing book. It just came out, and I started reading it yesterday. Its also excellent.

Posted: Thu Dec 14, 2006 5:20 am
by Ollie Saunders
You should check out the new head first OO analysis and desing book. It just came out, and I started reading it yesterday. Its also excellent.
ooooooo yes!
* searches *
hmmm must be very new, i can't find it on O'Reilly safari or Amazon but I will definitely look at that when I can track it down.

Posted: Thu Dec 14, 2006 7:35 am
by Begby
ole wrote:
You should check out the new head first OO analysis and desing book. It just came out, and I started reading it yesterday. Its also excellent.
ooooooo yes!
* searches *
hmmm must be very new, i can't find it on O'Reilly safari or Amazon but I will definitely look at that when I can track it down.

http://www.amazon.com/Head-First-Object ... F8&s=books