Is there a consensus on how plugins should be implemented in a PHP application?
I\'ve looked into the observer pattern which comes close, it\'s really just a notificati
Well, there's a link to the project called jin-plugin right in the Wikipedia article about Plugin concept. I am seeing this framework first time, too, but, maybe, you can use it right away.
Besides, you should really google for things like "Plugin Pattern", there's just two links I found on first page: Plug-in Pattern, Extensibility pattern (wikipedia).
If it's really a pattern, it should be language agnostic, so you can safely take any existing solution from any language and convert it to PHP.
P. S. Thanks for question, anyway, you really did raise my own interest in this topic. ;)
There is no consensus as in the Silver Bullet sense. For established patterns, you have multiple options like
to name a few.
Which you use is up to you, but you should make sure your system architecture supports the modularity. Have a look at these slides for some ideas
Take a look at the Yii framework. Yii heavily relies on events which are much cleaner than hooks, actions, etc. Using events, you can allow different parts of the system to talk to each other in an object oriented manner.
I think an Events Dispatcher is a nice and clean way to implement plugins, or any extension for that matter. An Events Dispatcher is an implementation of the Observer pattern, and is being used in symfony, Symfony2 and Zend Framework 2 (beta).
Looking through any of that source on github will make for some interesting reading. Though, an interesting bit of information can be found here:
http://components.symfony-project.org/event-dispatcher/trunk/book/02-Recipes
I wrote an Events and Hooks class a few years back for a project, I'll post that here if I can find it.
The way you've done this, with hooks is also how I implement this.
The biggest problem with your sample though, is that your function instantiates the plugin. Why not pass an instance of the plugin instead?
The way I've done this, is that a plugin gets instantiated first, and registers its hooks itself.
Zend Framework is using the dispatchLoopStartup() and dispatchLoopShutdown() hooks as class methods. Each plugin is a class that implements the aforementioned methods.
ZF manual reference