PHP中的配置文件(XML对YAML与PHP文件) [英] Configuration files in PHP ( XML vs YAML vs PHP files )

查看:284
本文介绍了PHP中的配置文件(XML对YAML与PHP文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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?

感谢您的帮助

推荐答案

我取决于谁会触摸那个配置。

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

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

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

如果是技术用户(例如,其他开发人员或系统管理员),那么有一个选择:复杂的配置文件将更好地与一个结构化文件,如XML或YAML,因为有较少的机会打破PHP代码,如果事情错误(你可以报告具体的解析错误,建议如何修复)。简单的选择可以用PHP编写(但是如果有人忘记了一个引用字符,程序将失败,出现奇怪的错误,或者如果错误只出现在日志中,根本没有错误。)。

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!).

如果是最终用户...那么根本不应该公开配置文件,在我看来。您需要提供一个安装程序,它将处理一切(并生成机器可读的配置文件或写入db)。

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).

这篇关于PHP中的配置文件(XML对YAML与PHP文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆