3

主脚本包括为其添加功能的“模块”。每个模块的设置如下:

<?php
//data collection stuff
//(...) approx 80 lines of code
//end data collection
$var1 = 'some data';
$var2 = 'more data';
$var3 = 'other data';
?>

每个模块都有完全相同的变量,只是数据收集不同。

我想知道将模块数据存储在 MySQL 中是否是一个合理的想法:

[database]
|_modules
  |_name
  |_function (the raw PHP data from above)
  |_description
  |_author
  |_update-url
  |_version
  |_enabled

...然后包含数据库中的 PHP 数据并执行它?类似于页面顶部的每个模块名称的选项卡导航系统,然后在每个选项卡中,页面内容将通过解析该function部分中模块的数据库存储代码来运行。

目的是节省代码空间(更少的行),允许轻松更新,并根据enabled选项包含/排除模块。这就是许多其他网络应用程序的工作方式,其中一些也是我自己的。但我从来没有这么深刻地思考过这个问题。这有什么缺点或安全风险吗?

4

3 回答 3

2

I would recommend against it. One of the disadvantages is that the code can not be cached by an op-code cache like e.g. APC. Furthermore it is not that easy to manage the code in a version control system. And later on when you want to have unit tests, continuous integration tests etc. it will be a lot more cumbersome too.

于 2012-07-10T10:46:47.667 回答
1

例如MODx使用这种方法。PHP“片段”和模块存储在数据库中。大多数这些 PHP 片段只是设置了一些配置值,然后包含来自服务器上文件的主要模块代码。

The main advantage is the flexibility in editing the module configuration as all modules may be edited in the backend of the CMS. On the other hand, Wordpress also allows to edit the plugin-PHP-code from within the backend, but stores everything as files on the server, without storing PHP in the database.

Well, not really an answer, but I think that is a matter of taste.

于 2012-07-10T08:23:41.743 回答
1

I don't think there is a hard and fast answer for this. The method you describe would benefit certain applications, usually small websites like a blog site for example.

The traditional way of keeping everything as files on a server would benefit larger applications due to the time taken to read the files on the server rather than retrieveing them from a database.

So it really depends on what you're trying to achieve really.

于 2012-07-10T09:41:41.903 回答