The vast majority of PHP applications out there use native PHP files for configuration. One or more standard PHP files declares a number of variables used for configuration. Given the syntax constraints of PHP – e.g. you can’t put a quote inside quoted values – a culture of developers insisting on managing config files is pretty much standard. But it doesn’t have to be.
YAML, or YAML Ain’t Markup Language, is a “human friendly data serialization standard”. It’s essentially a very basic format for storing data, and uses far less syntax than standard PHP. Here’s a sample of YAML:
Time: 2001-11-23 15:01:42 -5
An error has occurred.
As you can see, it’s almost like English; it’s as basic as you want. Of course, that’s not to say it doesn’t support complex structures – this example demonstrates the power of the format.
Now, this is all well and good, but what does it mean for us as developers? Well, quite a lot, it seems:
- Our configuration files can finally be read – and modified! – by anyone.
- We can finally really start using config files, instead of database values with complex admin panels
- Config files are no longer as error prone as they used to be; forgetting to escape a quote in a config value won’t accidentally bring down your live application.
- The marketing guy can change ad positions in the company’s website himself, without having to call the poor IT guy at 1am.
Of course, there are also performance considerations of interpreting a specialised format as opposed to using native programming code. Thankfully, spyc, one of the best PHP YAML interpreters, is pretty damn fast.
Now that we have reliable config files, why not use them? Next time you’re building an application, instead of building some complex config manager in the admin panel, consider just using YAML.