views:

317

answers:

2

Hi!

I am planning on doing a research on how to implement a plug-in architecture in PHP. I have tried searching the web for possible references, but I thought that maybe my search for a good reference would be faster and more relevant if I asked here.

Has anyone here tried using plug-in architecture for web projects?

Thanks, Erwin

+2  A: 

I have written wordpress plugins and the magic that they rely on is "Variable Function Names". For instance this is valid php, in which the function call phpinfo() will be called:

$func_name="phpinfo";
$func_name();

This allows developer to "Hook" function calls, as in override them with their own functions without having change the rest of the application. Linux Kernel modules are all about "hooking", they wouldn't work without this behavior.

Unfortunately for PHP variable function names are a disgusting hack that consumes a lot of resources. All functions in a name space are put in a list and this list has to be searched though before the function is called, this is O(log2(n)). Also keep in mind that Variable Function Names can not be accelerated properly in HipHop although the code will still be transformed into valid C++. A better approach would be to re-declare functions like you can in python which would be O(1) complexity, but the PHP development team HATES this idea (Yes, I've asked for this feature!).

Good luck!

Rook
This is a horrible method IMO.
davr
@davr: Why? As long as this is explicited and documented and it´s clear that it is the correct behavior, it´s completely fine. In an analogy it´s the same things as delegates (in C#).
Luiz Damim
@davr If you can come up with a better solution the entire PHP community would love to hear it.
Rook
Actually, sometimes I use variable function names in my code, when necessary. I didn't know that it would also be useful when it comes to plug-in architecture. Thanks guys for the info.@davr I'm just curious, do you have other suggestions as to how to implement this architecture in PHP?
Erwin Paglinawan
A: 

You could take a look at how Zend Framework implemented their Plugin Loader component.

Basically you set path´s to where plugins are stored and the loader tries to load the first plugin found in a LIFO way.

Luiz Damim
Sure, I'll do that. Thanks!
Erwin Paglinawan