如何包含一个PHP,然后删除它? [英] 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.
好的,这是我的情景:
pluginA.php
function info(){
return "Plugin A";
}
pluginB.php
function info(){
return "Plugin B";
}
最后,我有一个插件管理器,负责导入所有插件信息池数组:
Manager.php
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();
}
}
}
这里的问题是我打印池数组它只显示加载的第一个插件的信息。我认为文件包含覆盖了信息,因为它仍然从第一个包含调用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.
有没有办法包含两个插件的信息所有插件文件的名称相同的info()函数?
Is there a way to include the info of both plugins having the info() function with the same name for all plugins files?
提前谢谢
PS:致命的不能重新声明错误永远不会被抛出
PS: a fatal cannot redeclare error is never hurled
推荐答案
你可以用动态方式创建插件类
you can use the dynamic way to create plugin classes
插件类
class PluginA
{
public function info()
{
return 'info'; //the plugin info
}
}
经理类
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();
}
}
}
}
这篇关于如何包含一个PHP,然后删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!