I am currently writing a small PHP application for myself, for which I would like to add simple plug-in extensibility.
I found some ideas on how to do this, but I had the feeling that they were all too complex for my needs.
Let me explain exactly what I need:
My application should perform one simple task, for example: do a web search.
The user should be able to choose which plugin is used.
For example, you might have a Google, Yahoo, and Bing plugin to choose from.
Each plugin will have a "performWebSearch" function that returns search results.
Yes, thatβs basically it.
I can show you what code I am currently using to make it more clear:
To get a list of existing plugins:
$classes_before = get_declared_classes();
foreach(glob("../plugins/*.plugin.php") as $filename) include $filename;
$classes_after = get_declared_classes();
foreach($classes_after as $class)
{
if(!in_array($class, $classes_before))
{
$plugins_available[] = $class;
}
}
And this is what the βpluginβ looks like:
class google
{
public $name = "Google Search";
public $version = 1.0;
public function performWebSearch($query)
{
}
}
It works, but it feels "dirty", doing it that way.
I'm sure there is a way to improve the method, but I have no idea what it could be.
I appreciate any help. Thank.
source
share