PHP中的配置文件(XML与YAML对比PHP文件)

时间:2023-01-15 09:22:57

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:

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

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?

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

Thanks for your help

谢谢你的帮助

1 个解决方案

#1


8  

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.

如果它是开发人员,那么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!).

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

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

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

#1


8  

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.

如果它是开发人员,那么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!).

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

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

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