开发者

PHP Plugin loader which creates instance of plugins in a specific directory and which impelements plugin interface

开发者 https://www.devze.com 2023-03-18 05:44 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消