PHP Application Configuration with YAML

Posted by:

I have always disliked creating configuration data by reading from various formats into global arrays, so I needed to build or borrow a configuration class for my own projects that I could standardize on.

I wanted the following functionality.

  • Use the YAML format
  • Minimize load time by caching configuration data
  • Use object notation instead of array access notation.

I discovered various configuration classes but none of them quite had everything I was looking for so I put together the best code and ideas (for my purposes) that I found to create the Axial_Configuration class.

The Base

During my search for a configuration class I discovered the Zend_Config class which is a perfect fit as far as my object notation requirements. However Zend_Config lacks a YAML implementation and has no cache functionality. I decided to base my class on the Zend_Config ideas to achieve the object notation functionality and added on YAML and cache support.

The YAML format

If you are unfamiliar with the YAML format you probably want to read the following. I won’t go into the specifics as they are covered in depth elsewhere.

I discovered YAML while doing some research into various PHP configuration formats a while back and immediately clicked with it. I found two YAML parsers for PHP Spyc a simple php yaml class and the Syck extension. Most of my development is done with shared hosting in mind so the Syck extension isn’t an option for now.

Make it Faster

Since loading and parsing YAML in PHP is not the fastest operation I would love to use the Syck extension but in order to ensure compatibility in a variety of environments I implemented a caching scheme using var_export() and the magic method __set_state(). Testing shows a load time decrease of about 30x to 50x for cached data.

Try it out

I know there are dozens of other configuration classes out there but hey “variety is the spice of life. — William Cowper. I don’t have much in documentation for it but here is a little bit of code to show you basic usage.

  2. //create an instance that is read only and cached in the current directory.
  3. //you will need to make sure that the directory is writeable.
  5. $config = new Axial_Configuration_Yaml(‘config.yaml’,true,‘./’);
  6. echo ‘File: ‘.$config->file . ‘.<br/>’;
  7. echo ‘Purpose: ‘.$config->purpose . ‘.<br/>’;
  8. echo ‘Data: ‘.print_r($config->data->toArray(),true) . ‘.<br/>’;


  • PHP 5.1+

You can download the code and try it out. Let me know if you find it useful ¦ or not.


About the Author


  1. Shahar Evron  November 23, 2007


    I wrote a short article on how to use YAML with Zend_Config (it's pretty easy) some time ago:

    I really liked your caching idea – dumping the data using var_export should be extremely fast especially if you use opcode cache (APC, XCache, eAccelerator, Zend Platform etc.) because your system will have the stuff cached in RAM and won't even read it from disk.


  2. Doug Hill  November 23, 2007

    Now how in the world did I miss your article!?! Oh well, have to stick your site in the reader. I did run some benchmarks cached and uncached and its significanly faster cached. It varies but on average around 50x. I haven't tried syck to match them up though.


  3. joomla  November 29, 2007

    I can't become convinced to yaml.. still use xml :)

  4. Skander MABROUK  August 14, 2012

    take a look to symfony classes, it's very easy. you should just take the classe sfYaml and go !

  5. vitabrain  January 4, 2016

    Spot on with this write-up, I really think this amazing site
    needs far more attention. I'll probably be returning to read through
    more, thanks for the info!

Add a Comment

  1. roScripts Webmaster resources and websites  November 23, 2007
  2. ??????? » [Web] ????  November 24, 2009