PHP - 常量/全局变量/配置

发布于 2024-10-15 21:30:08 字数 684 浏览 3 评论 0原文

我有一个脚本(一个小型​​且简单的类似 CMS 的系统),我一直在处理该脚本并将其用于客户端站点。由于客户有不同的要求,我实现了一个模块系统,它允许我修改/或向 CMS 添加功能,而无需修改 CMS 脚本。

如何实现一个配置系统,允许我从模块更改主 CMS 的默认设置?

例如,CMS 默认有两个菜单: $menu = array('menu-1', 'menu-2'); 我如何从模块中覆盖此设置?

我想到的一种解决方案是使用常量并序列化/反序列化:

defined("BLA") or define("BLA", serialize(array(
 'boo' => 'stuff',
 'foo' => array('1', '2', '3'),
 'moo' => true,
 ...
)));

因此我可以轻松地在模块初始化函数中覆盖此设置,该函数在 CMS 中定义常量之前运行。

然后我在脚本中的任何地方使用这些常量,例如:

$bla = unserialize(BLA);
...
foreach(unserialize(BLA) as $key => $value)...

另一种选择是使用全局变量,但人们说使用全局变量不好。

那么对于我正在寻找的问题有更好的解决方案吗?

I have a script (a small and simple CMS-like system), which I'm always working on and use it for client sites. Since clients have different requirements, I've implemented a module system which allows me to modify/or add functionality to the CMS, without having to modify the CMS script.

How can I implement a configuration system that allows me to change the default settings of the main CMS from the modules?

For example the CMS has by default two menus: $menu = array('menu-1', 'menu-2');
How could I override this setting from the modules?

One solution I've thought of is to use constants and serialize/unserialize:

defined("BLA") or define("BLA", serialize(array(
 'boo' => 'stuff',
 'foo' => array('1', '2', '3'),
 'moo' => true,
 ...
)));

So I could easily override this setting in the module initialization function which runs before the constant is defined in the CMS.

Then I'm using these constants everywhere inside my scripts, like:

$bla = unserialize(BLA);
...
foreach(unserialize(BLA) as $key => $value)...

Another alternative would be to use a global variable, but people say it's bad to use global.

So are there any better solutions to what I'm looking for?

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

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

发布评论

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

评论(4

失眠症患者 2024-10-22 21:30:08

我建议使用带有静态变量的类。或多或少相同的结果,但不需要反序列化,并且您可以实际使用该变量,而不是临时变量。

// My Constants
class MCo { 
    public static $BLA = array(
        'boo' => 'stuff',
        'foo' => array('1', '2', '3'),
        'moo' => true,
        // ...
    );
}

echo MCo::$BLA['boo'];

foreach (MCo::$BLA as $key => $value) {
    // ...
}

编辑: ircmaxell 有一点,请考虑一下

// My Private Constants
class MPCo { 
    private static $_BLA = array(
        'boo' => 'stuff',
        'foo' => array('1', '2', '3'),
        'moo' => true,
        // ...
    );
    public static BLA() {
        return self::$_BLA;
    }
}

foreach (MPCo::BLA() as $key => $value) {
    // ...
}

I would recommend using a class with static variables. More or less the same result, but no need for unserializations and you can actually use the variable, not a temporary one.

// My Constants
class MCo { 
    public static $BLA = array(
        'boo' => 'stuff',
        'foo' => array('1', '2', '3'),
        'moo' => true,
        // ...
    );
}

echo MCo::$BLA['boo'];

foreach (MCo::$BLA as $key => $value) {
    // ...
}

EDIT: ircmaxell has a point, consider this then

// My Private Constants
class MPCo { 
    private static $_BLA = array(
        'boo' => 'stuff',
        'foo' => array('1', '2', '3'),
        'moo' => true,
        // ...
    );
    public static BLA() {
        return self::$_BLA;
    }
}

foreach (MPCo::BLA() as $key => $value) {
    // ...
}
窗影残 2024-10-22 21:30:08

如果您计划在数组中存储大量静态数据,为什么不将其存储在配置文件中呢?或者,您可以从数据库加载它,但配置对此更好。

另一种选择是按照@inti 在他的回答中建议的方式进行操作。

If it's going to be a lot of static data that you plan on storing in the array, why not store it in a config file? Alternatively, you could load it from a DB, but config is better for this.

And the other option is to do what @inti suggested in his answer.

白龙吟 2024-10-22 21:30:08

我要问的第一个问题是为什么你需要一个常量数组。您的问题可能还有另一种解决方案,它同样好,但不需要数组。

一种更好的替代方法是创建一个配置对象并将其传递。这样它仍然是可测试的,因为它注入了依赖项,并且不会对使用序列化产生性能影响。

$config = new StdClass();
$config->boo = 'stuff';

doSomething($config);

常量并不比全局变量好多少。事实上,它们在某些方面更糟糕,因为使用它们进行测试非常非常困难,因为一旦定义了常量,就无法更改它。因此,在追求良好的可测试代码时,常量并不好。如果您依赖于常量而不是处理幻数或文件系统路径,您可能需要重新考虑您的方法(即使幻数常量也可以使用配置类更好地处理)...

The first question I'd ask is why do you need an array in a constant. There is likely another solution to your problem that's just as good, but doesn't need arrays.

One alternative that would be better is to create a configuration object, and pass that around. That way it's still testable since it's injecting the dependencies and doesn't have a performance impact of working with serialize.

$config = new StdClass();
$config->boo = 'stuff';

doSomething($config);

Constants aren't much better than global variables. In fact, they are worse in some respects since it's very hard to test with them since once you define a constant, you can't change it. So in the pursuit of good testable code, constants are not good. If you're dependent on constants other than for dealing with magic numbers or filesystem paths, you might want to rethink your approach (Even magic number constants might be better handled with a configuration class)...

深陷 2024-10-22 21:30:08

这确实是不必要的,只需使用变量而不是常量,它会更简单、更清晰,而且我想性能会更好。

This is really unnecessary, just use a variable instead of a constant, it will be simpler, clearer and I would guess better performing.

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