Handling Plugins in PHP
blog.ircmaxell.com
blog.ircmaxell.com
Even though I'm a bit of an OO snob and use design patterns in my work, I also like the simplicity of WordPress's plugin system. I'm not even sure what it would be called but it's proven to be really effective with thousands of plugins.
As I said in my post, just because an application doesn't use classes, doesn't mean it's not OOP or borrowing OOP patterns (and Drupal is actually a pretty good example of that).
It's such a simple and easy thing to write into your code with almost no effort, yet if you do it for every function your app would wind up being extremely pluggable.
What I do and it works very well, is that I register plugins, callbacks in the application configuration:
$cfg = ['plugins_for_foo' => ['callback_one', 'callback_two',
[$Obj, 'three']]];
Then in the code I get my configuration like this: $callbacks = Conf::get('plugins_for_foo', array());
And I can iterate over them and activate them. It is fast, flexible, you can easily test the callbacks in your unit tests, etc. Note that this is the standard approach to load components with Django.Just because the pattern has a name, doesn't mean it can't be simple. In fact, a lot of the patterns are implemented all over the place under different names and with slight tweaks.
An important point is to understand the difference between the patterns, and know how to identify them. You can call a cow a lion, but it'll still moo just the same...
I use both Python and PHP extensively and I find interesting that Python developers try to go to the minimum that work while PHP developers try to go the nice conceptually "right" way. I wonder if this is because the PHP developers kind of need to prove that they can do rigorous OO programming with their language or something like that where the Python developers do not care and just do it the simple intuitive way.
I'm getting more used to using generic structures and anonymous functions as I write more Javascript but it bugs me sometimes having to look at the source code or API docs to remember what parameters are available for a method call.
I guess it's being lazy in a different way - I like to do more work upfront so I can be lazier later!