The best part of my favorite PHP framework, Zend framework is Zend_Config. With Zend Config you can run you web application with more power full configuration that any one can change your application setting for use.
Read more information about Zend Config at Zend framework manual for Zend Config.
But in most web application you may have many configuration file with special format such as INI, XML or PHP. Also some of configuration is for one part of your application and may you put in special folders.
My Zend framework folder structure
So this is my default Zend framework folder structure that you have see more configuration file.

Default solution
In default solution you need to define a Zend Config object for each file to access file data.
But with this solution you can globally access all files in any format in each folder.
My_Config class for solve better this problem
Look at my class
/**
* @copyright Copyright(c) Muhammad Hussein Fattahizadeh. All rights reserved.
* @author Muhammad Hussein Fattahizadeh
* @link <http://mhf.ir/>
*/
class My_Config
{
/**
* Base folder that contain config files
* @var string
*/
private $_baseFolder;
/**
* Create config object
*/
public function __construct($baseFolder)
{
// check for valid folder
if(is_dir($baseFolder)) $this->_baseFolder = $baseFolder;
else throw new Zend_Exception("Invalid base folder for configurations.");
}
/**
* Set the config object is return the error.
* @param string $name
* @param object $value
*/
public function __set($name, $value)
{
throw new Zend_Exception("Configurations is readonly.");
}
/**
* Get the config data
* @param string $name
* @return object
*/
public function __get($name)
{
// set the configuration file path and format
list($fileFormat, $configPath) = $this->_solvePath($name);
// switch format for get configuration type
switch ($fileFormat) {
case 'ini':
$value = new Zend_Config_Ini($configPath);
break;
case 'xml':
$value = new Zend_Config_Xml($configPath);
break;
case 'php':
$value = new Zend_Config(require $configPath);
break;
}
return $value;
}
/**
* Path solver
* @param string $pathString
* @return array
*/
private function _solvePath($pathString)
{
// set file format
$explodePath = explode('_', $pathString);
$fileFormat = end($explodePath);
if(!in_array(strtolower($fileFormat), array('ini', 'xml', 'php'))) throw new Zend_Exception("Invalid configuration file format.");
// process path
array_pop($explodePath);
$configPath = implode(DIRECTORY_SEPARATOR, $explodePath);
// return data
return array(strtolower($fileFormat), $this->_baseFolder . DIRECTORY_SEPARATOR . $configPath . '.' . $fileFormat);
}
}
Easy to use
Here this is an example for using this method for the best way for my solution for tree and multi format configuration file.
$config = My_Config('/path/to/project/application/configs');
// just dont forget last part of get method for config is file format look at the examples
// load '/path/to/project/application/configs/database/mysql/news.xml' and get node adapter data
echo $config->database_mysql_news_xml->adapter;
// load '/path/to/project/application/configs/database/mysql/articles.ini' and get node 'params->username' data
echo $config->database_postgresql_articles_ini->params->username;
Waiting for your comments friends
Be waiting for your comments for improve this solution. Maybe I add it into Zend framework as a new component.

My name is Muhammad Hussein Fattahizadeh. I have 24 years old. I study information technlogy engineering in Payam Noor University of Iran.
#1 by Henrique Mattos on June 21st, 2010
This looks great. I just read it and will try to imlement this later in my project. What I want to be sure is that only calling the path to project/app/configs would load configuration files even if they are some deeper leves?
To illustrate this:
$config = My_Config(’path/to/project/app/configs’);
would load the files in
– project/app/configs
– project/app/configs/database
– project/app/configs/database/sql
– project/app/configs/service
???
#2 by Muhammad on June 21st, 2010
Thank for reading my article.
Of course you can use it.
Just put this class in your library folder and rename it as your autoloadernamespaces and test it.
But do you ever use Cache for config file? Did you think it’s useful for better performance? I need your recommend to improve this class.
#3 by Tom A on June 21st, 2010
The understores in the echo are replaced with /…
$config->database_mysql_news_xml->adapter means the adapter setting in /config/database/mysql/news.xml
#4 by Muhammad on June 21st, 2010
Thank Tom A for commend. Example updated.
#5 by Sven on June 22nd, 2010
Nice example,
but why don’t you use an Config Resoucre Plugin, or port your Class to an resource Plugin.
Loading and generating Zend_Configs has a negative performance impact for your application the more configs you load and merge. Than you can use something like that for example:
if ($bootstrap->hasResource(’cachemanager’)) {
$bootstrap->bootstrap(’cachemanager’);
$manager = $bootstrap->getResource(’cachemanager’);
$cache = $manager->getCache(’config’);
}
and you can always retrieve the config object via the resource manager without loading all your files over and over.
#6 by Muhammad on June 24th, 2010
Yeah good idea. I will do it. Thank for reading my article.
#7 by Jeroen Keppens on June 27th, 2010
Hi
I like the idea. Apart from the caching/resource comment, which I agree 100% with, I think you should also have another look at the name to the config. The “XML” inside it is not clean and I wonder if it might be even nicer to have a level per dir:
Suppose you have these files:
configs/database/mysql/news.xml
configs/database/mysql/clients.ini
configs/database/oracle/reporting.xml
They could be accessible by:
$config->database->mysql->news->someConfigElement;
$config->database->mysql->clients->someConfigElement;
$config->database->oracle->reporting->someConfigElement;
Wkr
Jeroen
#8 by ed hardy clothes on September 8th, 2010
Thanks for your posting; I really appreciate your ideas. Hope you can keep going.
This is a really great website, and I really like your essay. Thanks for your sharing.