Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plugin Architecture in PHP

Tags:

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

like image 635
Erwin Avatar asked Feb 23 '10 00:02

Erwin


2 Answers

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!

like image 182
rook Avatar answered Oct 11 '22 11:10

rook


There are a lot of concepts that can be considered a 'plugin'. You can write a plugin system using:

  • Event dispatchers (see the Symfony Event Dispatcher)
  • Middlewares (see Silex middlewares)
  • Observer OO pattern (see Google / Wikipedia)
  • And many others
like image 29
Wouter de Winter Avatar answered Oct 11 '22 12:10

Wouter de Winter