简体   繁体   中英

How to include a php and then remove it?

Well, I don't know if this post have the correct title. Feel free to change it.

Ok, this is my scenario:

pluginA.php

function info(){
   return "Plugin A";
}

pluginB.php

function info(){
   return "Plugin B";
}

Finally, I have a plugin manager that is in charge of import all plugins info to pool array:
Manager.php

class Manager
{
    protected $pool;

    public function loadPluginsInfo()
    {
        $plugin_names = array("pluginA.php", "pluginB.php");

        foreach ($plugin_names as $name) 
        {
            include_once $name;
            $this->pool[] = info(); 
        }
    }
}

The problem here is that when I print pool array it only show me the info on the first plugin loaded. I supposed that the file inclusing override the info because it still calling the info() method from the first include.

Is there a way to include the info of both plugins having the info() function with the same name for all plugins files?

Thank you in advance

PS: a fatal cannot redeclare error is never hurled

you can use the dynamic way to create plugin classes

plugin class

class PluginA
{
    public function info()
    {
        return 'info'; //the plugin info
    }
}

manager class

class Manager
{
    protected $pool;

    public function loadPluginsInfo()
    {
        $plugin_names = array("pluginA", "pluginB"); //Plugin names

        foreach ($plugin_names as $name) 
        {
            $file = $name . '.php';
            if(file_exists($file))
            {
                require_once($file); //please use require_once
                $class = new $name(/* parameters ... */); //create new plugin object

                //now you can call the info method like: $class->info();
            }                
        }
    }
}

Are you sure the interpreter isn't choking w/ a fatal error? It should be since you're trying to define the info function twice here.

There are many ways to achieve what you want, one way as in @David's comment above would be to use classes, eg.

class PluginA
{
  function info() { return 'Plugin A'; }
}

class PluginB
{
  function info() { return 'Plugin B'; }
}

then the Manager class would be something like this:

class Manager
{
    protected $pool;

    public function loadPluginsInfo()
    {
        $plugin_names = array("PluginA", "PluginB");

        foreach ($plugin_names as $name) 
        {
            include_once $name . '.php';
            $this->pool[] = new $name();
        }
    }
}

Now you have an instance of each plugin class loaded, so to get the info for a plugin you would have $this->pool[0]->info(); for the first plugin. I would recommend going w/ an associative array though so you can easily reference a given plugin. To do this, the assignment to the pool would become:

$this->pool[$name] = new name();

And then you can say:

$this->pool['PluginA']->info();

for example.

There are many other ways to do it. Now that 5.3 is mainstream you could just as easily namespace your groups of functions, but I would still recommend the associative array for the pool as you can reference a plugin in constant time, rather than linear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM