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
https://forums.phpfreaks.com/topic/165669-php-hook-system/
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
https://forums.phpfreaks.com/topic/165669-php-hook-system/#findComment-873900
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
https://forums.phpfreaks.com/topic/165669-php-hook-system/#findComment-873947
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.