PHP "hooks"?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

PHP "hooks"?

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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!'
*/

?>
User avatar
theFool
Forum Newbie
Posts: 17
Joined: Thu Oct 26, 2006 2:00 am
Location: Berlin, DE

Post 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.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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
Post Reply