Re: Building a “modular” PHP site

This is a very old article. It has been imported from older blogging software, and the formatting, images, etc may have been lost. Some links may be broken. Some of the information may no longer be correct. Opinions expressed in this article may no longer be held.

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:

\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:

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.