PHP 中基于模块的系统 - 如何?

发布于 2024-12-02 10:09:30 字数 1219 浏览 0 评论 0原文

我有一个 PHP Web 应用程序,我在其中制作了一个小模块系统,其基本工作原理如下:

每个模块都有自己的文件,文件名可以是任何内容,但并不重要(为了逻辑起见,我将文件名命名为与模块名称相同)。模块的内容可能如下所示:

$MODULE["name"] = "some_module";
$MODULE["description"] = "This module does some things";
$MODULE["required"] = false;
$MODULE["module_specific_var"] = some_function_that_returns_some_value();

我有一个全局 $MODULES 数组,我将所有模块读入其中:

foreach ($modules_list as $module_filename){
  if (isset($MODULE)) unset($MODULE);
  include($module_path."/".$module_filename);
  $MODULES[$MODULE["name"]] = $MODULE;
}

现在这非常简单,并且已经足以满足我的需求...直到现在...现在我需要一个模块特定的函数,并且不适用于关联数组(据我所知)。我想要的是能够调用 $MODULES["name_of_module"]["name_of_module_specific_function"]();

我知道这可以通过函数引用来实现,只要我对每个模块使用 include() ,模块文件中定义的任何函数都将在全局空间中可用 - 但这可能会导致命名问题,所以是个坏主意。

或者,我将每个模块设置为一个类,大致如下:

class module_name {
  var $name = "some_module";
  var $description = "This module does some things";
  var $required = false;
  var $module_specific_var = some_function_that_returns_some_value();
  function module_specific_function(){
  }
}

由于我已经有了相当多的代码,我的问题是 - 是否有另一种方法可以使所有模块基于类,从而避免名称冲突的问题并允许我保持当前的模块系统相当完整?

I have a PHP web application where I've made a little module system, that basically works like this:

Each module has it's own file, filename can be anything, but does not matter (for the sake of logic I named the filename is the same as the module name). The content of a module could look like this:

$MODULE["name"] = "some_module";
$MODULE["description"] = "This module does some things";
$MODULE["required"] = false;
$MODULE["module_specific_var"] = some_function_that_returns_some_value();

I have a global $MODULES array where I read all modules into:

foreach ($modules_list as $module_filename){
  if (isset($MODULE)) unset($MODULE);
  include($module_path."/".$module_filename);
  $MODULES[$MODULE["name"]] = $MODULE;
}

Now this is pretty simple and have been more than enough for my needs... Until now... Now I need a module specific function and that doesn't work with associative arrays (as far as I know of). What I'd like is something along the lines of being able to call $MODULES["name_of_module"]["name_of_module_specific_function"]();

I know this can be achieved with function references and as long as I use include() for each module, any functions defined in the module files will be available in global space - this may cause naming issues though, so bad idea.

Alternatively I make each module a class, something along the lines of:

class module_name {
  var $name = "some_module";
  var $description = "This module does some things";
  var $required = false;
  var $module_specific_var = some_function_that_returns_some_value();
  function module_specific_function(){
  }
}

Since I've already got a fair bit of code, my question is - is there an alternative to making all the modules class based, which avoids both the problem of name conflicts and allows me to keep the current module system fairly intact?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美胚控场 2024-12-09 10:09:30

OO 是通往这里的道路。创建一个 Module 基类,如果您需要特定于模块的功能,则可以扩展该基类。

如果将类名映射到文件名,您甚至可以使用自动加载器,这样您就不必 include() 所有内容。最好有一个中心 Singleton 类来跟踪您的模块实例(以避免必须使用全局变量,这是一种已弃用的做法)。

OO is the way to go here. Make a Module base class, and extend that if you need module specific functions.

If you map the class names to your file names, you can even use an autoloader so you won't have to include() everything. It would also be nice to have a central Singleton class to keep track of your module instances (to avoid having to use globals, which is a deprecated practice).

怕倦 2024-12-09 10:09:30

我认为像 CommonJS 或 ES6 PHP 模块这样的东西会很好。对此已有一些想法:https://groups.google.com/forum /#!topic/commonjs/sVkxbQtR-lw

I think something like CommonJS or ES6 Modules for PHP would be nice. Already some ideas on that: https://groups.google.com/forum/#!topic/commonjs/sVkxbQtR-lw

想念有你 2024-12-09 10:09:30
  1. 基于普通函数的模块系统很难维护 - 使用对象
  2. PHP,因为版本 5 可以通过名称自动加载模块
  3. 使用分层系统 - 从抽象模块或接口,您将知道该模块可以做什么。
  4. 如果变量中有名称,则可以创建这样的实例 - $foo = new $bar()

其中 $foo 是新实例,$bar 存储对象名称

  1. Modules system based on plain functions is hardly maintinable - use objects
  2. PHP since version 5 can autoload module by its name
  3. Use hierarchical system - from abstract module or interface, you will know what this module can do.
  4. If you have name in variable you can create instance like this - $foo = new $bar()

where $foo is new instance and $bar stores object name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文