PHP 中的配置文件(XML、YAML、PHP 文件)

发布于 2024-09-30 05:53:20 字数 184 浏览 1 评论 0原文

我正在做一个小实验。在过去,大多数人会将配置文件作为一堆常量或具有大量属性的类保存在 php 文件中。现在,我们看到像 symfony 这样的项目使用 yaml 或其他东西。我的问题是:

如果您可以选择三种不同的方式来存储您的配置文件:xml、yaml 或 php 文件,您会选择哪一种?最重要的是:为什么?

感谢您的帮助

Im making a small experiment. In the old days, most people would save configuration files in a php file as a bunch of constants or a class with a loooot of attributes. Now, we see projects like symfony using yaml or something else. My question is:

If you could can pick from three different ways to store your config files: xml, yaml or php files, which one would you pick? Most important: Why?

Thanks for your help

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

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

发布评论

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

评论(1

只涨不跌 2024-10-07 05:53:20

对我来说,这取决于谁将接触该配置。

如果是开发人员,那么 PHP 文件是最好的,因为它们不需要任何额外的解析。

如果是技术用户(例如其他开发人员或系统管理员),则可以选择:复杂的配置文件最好使用结构化文件,例如 XML 或 YAML,因为如果出现问题,破坏 PHP 代码的机会较小(您可以报告特定的解析错误以及如何修复的建议)。可以使用 PHP 编写简单的选择(但在这里,如果有人忘记了引号字符,程序将失败并出现奇怪的错误,或者如果错误仅记录到日志,则根本没有错误!)。

在我看来,如果是最终用户......那么根本不应该公开任何配置文件。您需要提供一个安装程序来处理所有事情(并生成机器可读的配置文件或将内容写入数据库)。

For me it depends on who is going to touch that configuration.

If it is developers, then PHP files are the best, as they do not require any additional parsing.

If it is technical users (for example, other developers, or sysadmins) then there is choice: complicated config file would better go with a structured file, like XML or YAML, as there is less chance to break the PHP code if something goes wrong (and you can report a specific parsing error with suggestions how to fix). Simple choices can be written with PHP (but here if someone forgets a quote character the program will fail with strange errors, or with no errors at all if errors go to the log only!).

If it is final users... then no configuration files should be exposed at all, in my opinion. You need to provide an installer which will handle everything (and generate the machine-readable configuration files or write things to the db).

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