Creating dynamic plugins for PHP Classes

Ah … silly me. I somehow got confused about Plugin and Plugins - the little s makes the difference. It makes sense to me now. So the Plugins (with a suffix s) is essentially just a collection of Plugin. I suggest renaming it to something like PluginCollection to avoid confusion.
I would have suggested that Plugin could be scrapped and replaced with a callback. I can see your point in making them pluggable per default, rather than having to create new Pluggable’s around each Plugin. I’m not entirely convinced of this being the best solution, since it adds a semantic overhead over just using a callback in the trivial cases. I haven’t used it though, and I suspect the crux is how often you’ll need to re-wire the Plugins (without s) as Pluggables. How’s your take on that ?

Ah yes, for very small applications it might be too much overhead. Though for larger ones I believe it would be acceptable, especially if the application has numerous plugin collections and numerous plugins per collection. Though none of the overhead of plugins also being Pluggable comes into play unless the Plugin makes a pluggable call.

The other thing is that for the trivial cases, one could easily bypass the __call magic and overhead by calling the normal methods and manually calling the necessary point cuts or hooks inside them, somewhat similar to d11wtq’s first example, you still have this option.

The solution still needs work, no doubt. I’ve already got a number of things that I know could be done better. I’ve started working on some of them. Working on removing the necessity for objects to be “Pluggable” in order to have plugins interact with them right now.

PS. Thanks for the tip about renaming Plugins. I see how that could be confusing.

I’ve been working on this some more recently. I’ve made it now that any Object can be made pluggable; so you’d have an instance of Pluggable with a given PluginCollection; then you simply make the pluggable calls like $pluggable->doSomeMethod($object, $param-1, …, $param-n).

It’s a bit awkward at first maybe, but I think that is quickly overcome by the fact that anything can be dynamically made pluggable. This is where the real flexibility lies.

I’ve made a bit of a generic flowchart of the magic wiring that goes on when you make the call:

The only major issue I can see is the question of how much overhead is introduced, and though I’ve only done small tests, I’ve been very pleased with the results. For me, the flexibility outweighs the little bit of overhead.

In any case, I think I’ll probably brush it up some & release the API to the community… you know, share & share alike & all that jazz :wink:

Hi Dreamscape. This was a great read. I was wondering if you have an update to your plugin system?

check this post out

http://www.flyingwithfire.com/2006/10/23/simple-hooks-system/

You may have a problem with the ’ things as wordpress messed them up

http://rafb.net/p/0nkh8l85.html
http://rafb.net/p/6zo1Tq74.html

Those are the correct files with the ’ changed so it works

Sure do: http://code.google.com/p/phpplexus/ (link is in my sig too)