简体   繁体   English

为什么我的PHP插件系统无法正常工作?

[英]Why won't my PHP plugin system work?

I'm trying to make a prototype of a simple plugin system I plan on implementing into one of my projects. 我正在尝试制作一个简单的插件系统的原型,计划将其实施到我的一个项目中。 I have 4 files: 我有4个文件:

  • Index.php
  • Plugins/__BASE.php
  • Plugins/Sample.php

The index file checks if the 'oncall' method belongs to a class in the Plugins folder using functions I defined in the Plugins class ( __BASE.php ). 索引文件使用我在Plugins类( __BASE.php )中定义的函数检查'oncall'方法是否属于Plugins文件夹中的类。 If it does exist, it will execute it. 如果确实存在,它将执行它。

require_once 'Plugins/__BASE.PHP';

$func  = 'oncall';
$plugins = new Plugins();

if($plugins->IsPluginMethod($func)) {
    $obj = $plugins->GetObject($func);
    call_user_func(array($obj, $func));
}
else
    echo "'$func' isn't part of a plugin!";

__BASE.php is the base plugin class which all of the plugins will extend. __BASE.php是所有插件都将扩展的基本插件类。 It has two methods: IsPluginMethod() and GetObject() . 它有两个方法: IsPluginMethod()GetObject() IsPluginMethod checks if the method name supplied belongs to a class and GetObject returns an instance of the class the method belongs to. IsPluginMethod检查提供的方法名称是否属于一个类,并且GetObject返回该方法所属的类的实例。

class Plugins {

    public $age = "100";

    public function IsPluginMethod($func) {
        foreach(glob('*.php') as, $file) {
            if($file != '__BASE.php') {
                require_once $file;
                $class = basename($file, '.php');
                if(class_exists($class)) {
                    $obj = new $class;
                if(method_exists($obj, $func))
                    return true;
                else
                    return false;
            }
        }
    }
}

    public function GetObject($func) {
        foreach(glob('*.php') as $file) {
            if($file != '__BASE.php') {
                require_once $file;
                $class = basename($file, '.php');
                if(class_exists($class)) {
                    $obj = new $class;
                    return $obj;
                }
            }
        }   
    }
}

Sample.php is a sample plugin which prints $this->age which is defined in the Plugins class. Sample.php是一个示例插件,它打印$ this-> age(在Plugins类中定义)。

class Sample extends Plugins {

    public function oncall() {
        echo "Age: {$this->age}";
    }
}

This is what I see in index.php: 这是我在index.php中看到的:

'oncall' isn't part of a plugin!

Can anyone help? 有人可以帮忙吗? Thanks. 谢谢。

在__BASE.php文件中,将(glob('*.php')更改为glob('Plugins/*.php')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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