I implemented dynamic loading of plugins in the following way:
function processPlugin( $plgFile, $db ) {
require_once( $plgFile );
$plgin = new PlginImpl();
$plgin->setDb($db);
$ret = $plgin->process();
return $ret;
}
Each plugin defines a class named PlginImpl, which works fine. But it should be possible to call further plugins specified within within the return value of process(). That would call the same method specified above, but fails with:
Fatal error: Cannot redeclare class PlginImpl in ..
Please note that each plugin is a class, i.e.:
class PlginImpl extends Plugin implements PluginInterface
Plugin offer some useful functions while PluginInterface defines i.e. process().
I assume that the fact that all plugins are named PlginImpl causes the problem, hence my question: is there a way to unload a class (PlginImpl) after loading it with require_once? Or is there an entirely different approach I should follow?
EDIT
I tried without succeeding the following things:
- unset
$plginafterprocess() - calling
__destruct()– it doesn’t work neither withinprocessPlugin()nor within theprocessmethod
Since you can’t unload a class after you’ve loaded it, the only option you have is to rename each plugin.
PluginX, PluginY, etc., but it shouldn’t matter as you can just force them to use the plugin interface as you showed.
To load a specific plugin, you could simply have something like solomongaby suggests, but instead of a filename, you pass it the name of the plugin.. something like this: