Published by nick on 22 Sep 2007 at 11:46 am
Don’t use YAML for PHP, use parse_ini_file
YAML is a syntax for configuration files.
Huh?
Why on earth do we need another technology for config files?
I first ran across YAML while working with Symfony. YAML’s use in PHP is especially troubling, because PHP has a built in function for parsing config files, parse_ini_file(). This config file syntax is the same as the php.ini file, so it is well known by all PHP system administrators. It’s human readable, supports basic name/value pairs, and allows for comments.
It’s also high performance, since it’s a built in function, config files are parsed with the speed of C instead of text processing with PHP. This may not matter much for Joe Bloe’s blog website, but when you have lots of users, this makes a difference.
From what I saw with it’s use within symfony, everything could have been easily done using parse_ini_file(), and to make matters worse, when it was noticed that there where performance problems with parsing YAML, the Symfony authors decided to add a caching layer. Great. More complexity. This violates the number one rule of good software: Simplicity.
The Symfony authors should have used an existing technology that was built into PHP for handling config settings. Don’t make the same mistake.
nick on 22 Oct 2007 at 9:18 am #
This is being discussed here:
http://codingforums.com/showthread.php?t=126020
Clint on 23 Oct 2007 at 7:56 am #
yaml is basically impossible to parse in bash. Its not easy in perl (but of course, doable). ini files, however, are dead simple to parse in bash and perl.
When you have a heterogeneous environment, having one spot for local config information is a huge advantage.
she on 14 Feb 2008 at 1:45 pm #
Is your comment correct?
Yaml can store hashes and arrays, I dont think .ini can do.
Nick J on 22 May 2008 at 12:12 am #
One problem with parse_ini_file is backwards compatibility, because until at least March 2007 you could not escape quotes in INI files.
E.g. if you wanted to use an INI file to do this: $array[’x'] = ‘The message said "Hello World!"…’;
… then until just over a year ago, you could not do that due to a bug - i.e. an INI file like this:
————
x = "The message said \"Hello World!\"…"
————
… would not work with parse_ini_file. Not being able to have quotes in strings is a pretty fundamental limitation, so for apps that have to support PHP5.x, that rules out using parse_ini_file.
NOTini on 24 Sep 2008 at 7:48 pm #
sometime I wanna define a dsl style config file. the ini file looks like an ugly guy.
it’s depends on the requirement. in my project, there are tons of config files with dsl style
so I have no choice.