How to write simple plugin support in PHP?

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.

+4
source share
2 answers

, , :

. .

. [].php . [], [] - , Google.

, $engineName , :

include "your_folder/$engineName_search.php";

:

$searchClass = "Search$engineName";
$engine = new $searchClass();
$result = $engine->performWebSearch($search);
0

All Articles