Configuration files in PHP ( XML vs YAML vs PHP fi

2019-07-02 06:02发布

问题:

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

回答1:

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