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.
Rob Desbois on 01 Dec 2008 at 10:06 am #
As stated by ’she’ the ini file format doesn’t support anything more complicated than (name => value) pairs.
My application requires some data which is in a structured tree format - impossible to represent in an ini file. With YAML it’s easy.
Granted there are other formats which can support it - XML being the obvious one that springs to my mind, but I dislike the overheads of the structure and the unfriendliness to someone not well-versed in it. YAML is easy to read and easy to write whether you are man or machine, and whether you are a developer or not.
ananda on 08 Dec 2008 at 7:52 pm #
What about MVC ?
what when the data comes from a database instead of a file ????
Eric J on 25 Jan 2009 at 6:50 pm #
Shouldn’t this read, "Don’t use YAML for configuration files in PHP"? I’ve found YAML and SPYC to be very handy for non-configuration purposes.
counterpoint on 08 Apr 2009 at 3:58 am #
For a long time the use of .ini files in PHP seemed to me a non-starter on the grounds that a syntax error in the .ini file would cause script processing to terminate. Not the parse_ini_file function, but the entire PHP script. It’s in the nature of things that people will sometimes mess up configuration files. So how would you cope with this - parse the ini file using string processing before submitting it to parse_ini_file?!
More recently the function has returned an empty array or latterly false if there is a syntax error. But even that means that no diagnostic can be given if the .ini file is in error. This seems unacceptable in any system that aims to be user friendly, even when the user messes up.
YAML, XML, RelaxNG have the advantage of being a consistent and mutually supportive set of standards that support powerful configuration storage options.
Darkhogg on 31 Jul 2009 at 3:26 am #
Another option for tree-structured data is JSON. PHP comes with json libraries by default, and is a pretty simple syntax you can write directly and can be shared through every language.
Since json_decode() is a built-in function, it shouldn’t be slow.
other option is using some sort of tree.structuring INI, such as Parent.child, and then in PHPH parse out the dots to make an array like $thing[’Parent’][’child’] … that can be slow too, but maybe quickier than YAML
Even on 27 Sep 2009 at 5:47 am #
Yes, JSON is a fair choice as stated Darkhogg. The best part about JSON is that YAML is an extension of JSON so everything written in JSON is a correct file for a YAML parser. It might be a very good point for a lot of people. But I personally love some parts of the languages extension brought by the YAML. The good way would be to have YAML support by default in PHP…