04/04/2007: Re: Building a “modular” PHP site
Tyno Gendo wrote:
I have been pondering over building a “modular” site which accepts add-ons built by other people. I was wondering if anyone has any links to any reading material on how you build this kind of facility into your site?
The basic technique is this:
Firstly, provide a plugin registration function, which we’ll call, say, “plugin_register”. When a plugin is loaded, it will call your plugin_register function and tell your site at least the following information:
- How to use the plugin — i.e. provide a function name or a class name that the site can use to access the functionality of the plugin;
- When to use the plugin — this is normally done via a named hook.
So a particular plugin might be defined like this:
<?php
function tobys_plugin ()
{
echo "<!-- Hello World -->\n";
}
plugin_register('tobys_plugin', 'onpagefinished');
?>
Your plugin_register function would then add “tobys_plugin” to a list of functions that need to be run when the page has finished outputting.
Then in the rest of your code, add you hooks. For example, at the end of each page, you’d have:
<?php
run_plugins('onpagefinished');
?>
where run_plugins looks at the list of registered plugins and runs the ones that have registered using that hook.
That’s the simplified version. In real life, to allow the plugins to be more useful, you’ll often want to pass them particular parameters, such as the current URL, the login name of the currently logged in user, etc. I’ll leave you to figure that out on your own.
Comments
Comment 16 by Anonymous Coward at 30/01/2008 09:25
Ah, i would love more details! I just can’t find anything on modular application development.
Thanks for the short article though.
Date: Wednesday, 30th January 2008, 9:25am (UTC)
Comment 25 by Anonymous Coward at 28/02/2008 11:43
Great article! You gave me the basis to understand the plugin system and start developing this way. Thanks man!!!
Date: Thursday, 28th February 2008, 11:43am (UTC)
Comments are moderated and may take one or two days to show up on the site. You can bypass comment moderation by signing up as a registered user.