The state of YAML in PHP - Fabien Potencier: "
My first exposure to YAML was in 2001, back in the days when I was mainly
working with Perl. Well, I was not using YAML per se at that time, but rather
Data::Denter
, a
Perl library that provides data serialization/deserialization. I used this
library mainly for debugging purposes. From its documentation:
'It formats nested data structures in an indented fashion. It is optimized
for human readability/editability, safe deserialization, and (eventually)
speed.'
At the end of the year 2002, the module was deprecated in favor of a new
serialization language, YAML, with the added bonus of
being programming language independent. I promptly switched to use the Perl
YAML module, and I
never looked back. I used YAML as a mean to debug my Perl programs, but I also
started to use it more and more to store configuration data.
When I started to use PHP at the end of 2004, one of the first thing that
quickly bothered me was the poor support for YAML in the PHP world.
By the way, if symfony uses YAML a lot, it has nothing to do with Ruby on
Rails ;) It just happens that Ruby also has some Perl heritage!
But first, what is YAML?
According to the official YAML website, YAML (YAML Ain't Markup Language), is
a human friendly data serialization standard for all programming
languages.
YAML can be used to describe both simple and complex data structures. It's an
easy to learn language that describes data. As PHP, it has a syntax for simple
types like strings, booleans, floats, integers, arrays, and even more complex
ones like objects.
Nowadays, YAML is a heavily used format for configuration files, mainly
because even non programmers are able to understand and modify YAML files
easily.
To sum up the benefits of YAML, I often say that YAML files are as
expressive as XML files and as readable as INI files.
Since the creation of YAML, another lightweight data-interchange format has
come to life: JSON. JSON is quite similar to YAML (and as
a matter of fact, JSON is a subset of YAML); but even if it is easy for humans
to read and write, I think it is not as readable as YAML, and a bit too
verbose.
YAML
If you already know what is YAML and how to use it to describe your data
structures, just skip this section.
Besides strings, Booleans, and numbers, let's have a look at one of the
simplest configuration structure you can describe with YAML:
key: value
foo: bar
The above snippet is the simplest way to express key/value pairs in YAML. The
foo
key has a bar
value. The equivalent PHP code would be:
array('key' => 'value', 'foo' => 'bar')
And that's pretty much covers what you can do with ini files. Speaking of ini
files, you can also group key/values under 'sections'. Here is how this is
possible with YAML:
section1:
foo: bar
section2:
bar: foo
The equivalent PHP code reads as follows:
array(
'section1' => array('foo' => 'bar'),
'section2' => array('bar' => 'foo'),
)
That does the trick because there is several ways to describe key/value pairs.
The short notation (foo: bar
), and the expanded one, where you use
indentation to describe nested structures as above.
The same data structure can also be described as follows:
section1: { foo: bar }
section2: { bar: foo }
The {}
is how you enclose a hash. That's one of the greatest benefit of YAML
as a description format: you can visually organize yo
Truncated by Planet PHP, read more at the original (another 7619 bytes)
"