PHP Plugin loader which creates instance of plugins in a specific directory and which impelements plugin interface
how can i create with php an instance of all plugins which implements the plugin interface an which are saved in a specific plugin directory.
I did something in c# like that
Assembly assembly = Assembly.LoadFrom(pFileName);
foreach (Type type in assembly.GetTypes()){
if (type.IsPublic){
if (!type.IsAbstract){
Type typeInterface = type.GetInterface(pTypeInterface.ToString(), true);
if (typeInterface 开发者_运维问答!= null){
try{
object plugin = Activator.CreateInstance(pluginInterfaceType);
Is there a way to translate this in php?
Whats the easiest and securest way to realise this?
greetings
As a dynamic language PHP makes this a lot easier. As soon as you have the actual class name as a string, you can instantiate it. Then you just have to check if the instance implements the expected interface:
$pluginClass = 'My_Plugin';
$instance = new $pluginClass();
if(!$instance instanceof My_Interface) {
throw new Exception(get_class($instance) . ' does not implement My_Interface');
}
To load the class you should read up on autoloading classes in PHP. If you want to have all classes in a specific plugin folder available you just require_once any *.php file in that folder.
精彩评论