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.
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.
- PHP 5.1+
You can download the code and try it out. Let me know if you find it useful ¦ or not.