Jump to content

PHP Hook System


sjwspud

Recommended Posts

Got a quick question...

 

I have been searching Google for quite some time now trying to completely figure out how to use a PHP hook system. I am in the process of writing my own open source bulletin board system and want to allow hooks to be used.

 

What I basically want to do is add a hook system so users will be able to add plugins to the software to add different features basically and to even maybe alter current features in the software.

 

I have been trying to find a tutorial that would explain how to do this, but can't seem to find a single tutorial that really explains the hook system.

 

Could anyone direct me to a tutorial explaining the hook system? Thanks :-)

Link to comment
Share on other sites

Hooks are a very simple process, but they may not actually be what your after.

 

In the case of hooks, you simply call hook functions at certain places during your applications process.

 

A very simply example.

 

<?php

echo "start script";

echo "middle script";

echo "endscript";

?>

 

Now, lets say you wanted to make hooks available to start, middle, and end.

 

 

<?php

if (function_exists('start_hook')) {
  start_hook();
}
echo "start script";


if (function_exists('middle_hook')) {
  middle_hook();
}
echo "middle script";

if (function_exists('end_hook')) {
  end_hook();
}
echo "endscript";

?>

 

Now your users simply need to create start_hook(), middle_hook()  and end_hook() and place them in the correct location.

 

This is a very simplified example, but should get you started.

Link to comment
Share on other sites

Hooks are a very simple process, but they may not actually be what your after.

 

Seems pretty simple to use. You said they may not be what I am after...what would I need to do to allow plugins? Any ideas?

 

Thanks for your quick response. :-)

Link to comment
Share on other sites

Implementing a plugin system would entirely depend upon how your application is designed. At there simplest, hooks might do, otherwise you'll want to look more complex design patterns.

 

Theres a few threads on these boards that go into a bit of detail on the subject, try searching.

Link to comment
Share on other sites

If you enter define:plugin as a search term into google you'll get an explanation of what a plugin actually is, simply put: it's something a system hasn't yet have. I never created a plugin system myself and my best guess would be that a plugin would be registered for a particular section in your website, then when you enter that particular section it calls all registered plugins (somewhat like an observer pattern, i guess) and passes it's contents.

 

But, like I said I never created it myself before, maybe someone who wants to add something to that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.