Skip to content

Instantly share code, notes, and snippets.

@igorw
Created August 24, 2010 15:52
Show Gist options
  • Save igorw/547780 to your computer and use it in GitHub Desktop.
Save igorw/547780 to your computer and use it in GitHub Desktop.

Explaining hooks

Most of you have probably already seen the blog post regarding the RFC process for hooks to be added to ascraeus (phpBB 3.1). And many of you may not know what a hooks system actually is, so I will take the time to explain it.

In olympus (phpBB 3.0) all new additions to the core must be done by editing or patching the core files. The MODification is either installed manually or by using AutoMOD. What a MOD usually does is add some specific code at a specific location, for example database queries, template assignments or loading of language files. This approach worked for 2.0 and it works for 3.0, but frankly it produces a horrible mess. Installing MODifications is a great pain, uninstalling them is almost impossible in some cases.

To solve this issue we are introducing a hooks system. The hook system defines code injection points, giving each one of them a unique identifier. The hooks manager allows MODifications to bind to a specific code injection point. When that point is reached in execution, the hooks manager will call all of the hooks that registered, passing in the context that can be read and/or modified by the hook, thus "injecting" the hook into that location.

Here is an example with code. Note that this API may change.

// create the hooks manager
$hook_handler = new phpbb_hooks_controller();

// create a custom function
// will hook into user_add, just before the user is added
function hook_user_add_logging(&$row)
{
    file_put_contents('registered_users.txt', time() . ": " . $row['username'] . "\n", FILE_APPEND);
}

// register the hook
$hook_handler->add_hook('user_add', 'hook_user_add_logging');

// inside user_add()
// call hooks
$hook_handler->invoke_hook('user_add', $row);
@katanacrimson
Copy link

I feel so enlightened.

@igorw
Copy link
Author

igorw commented Aug 24, 2010

WIP, you fool.

@katanacrimson
Copy link

I couldn't resist. :>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment