In most of the Internet applications there is a need to have a configuration file to store static information that the application needs to work. An very common example of this information is credentials to connect to a database.
There are a lot of ways you can store this information and make it accessible to the application. Zend framework includes four classes for parsing four different kind of configuration files: Zend_Config_Ini, Zend_Config_Json, Zend_Config_Xml and Zend_Config_Yaml. In this article we will focus on Zend_Config_Ini for parsing files in the INI format.
The INI format
The INI format allows us to have represent hierarchy using the dot (.) and sections between brackets ([]). This is an example of INI file:
1
2
3
4
5
6
7
8
[section 1]
property1 = val1
property2 = val2
; This is a comment
[section 2]
property3 = val3
index1.property4 = "value four"
There are some things to notice from the INI format:
– Semicolons (;) start comments
– Lines don’t need to be ended with semicolon (;). Since the semicolon starts a comment it doesn’t affect the line, but it shouldn’t be used unless it is a comment
– Strings don’t need to be wrapped in quotes (“) unless they contain special characters
– You can create hierarchy by using the dot (.) operator (index1.property4)
– You can divide your file into sections using brackets ([])
Zend_Config_Ini
The way you parse an INI file with Zend_Config_Ini is by calling it’s constructor. This is the structure of the constructor:
1
public function Zend_Config_Ini($filename, $section, $options = false)
Where
$filename – Path to file to load
$section – The section of the INI file to load (the name between brackets ([]). Setting this parameter to NULL will load all sections. An array of section names can be passed to load multiple sections
$options – Configuration array. It supports two options:
allowModifications – If true the returned object can be modified
nestSeparator – Sets character to be used to separate hierarchies. By default it is the dot (.)
Zend_Config_Ini uses parse_ini_file() to parse the files, so you may want to read the documentation of that function if you find any weird behavior.
Zend_Config_Ini allows a section to inherit from another section using a colon (:):
1
[section2 : section1]
In the previous example section2 inherits from section1. We will see the usefulness of this in our next example.
Example
One of the most common uses of a configuration file is to load database credentials. This also allows us to make a good use of sections by having two different sections, one for development and another for production. Lets use this INI file as our base:
1
2
3
4
5
6
7
8
9
10
11
12
[general]
database.user = user
database.name = name
[production : general]
database.host = dbhost.com
database.user = differentuser
database.pass = verysecret
[development : general]
database.host = devdbhost.com
database.pass = secret
In this example both production and development inherit the database name and user from general, but production overwrites the user with one of its own.
If we were going to run our application in the development environment we would do something like this:
1
$config = new Zend_Config_Ini('/path/to/config.ini', 'development');
Then we could access our configuration data from the $config object like this:
1
2
3
4
$config->database->host; // devdbhost
$config->database->user; // user
$config->database->pass; // secret
$config->database->name; // name
We can see that parsing an INI file with Zend_Config_Ini is very easy.
Iterating a configuration file
Zend_Config extends the iterator interface, which allows us to treat the parsed configuration object as an array. This can be useful if you want to have an infinite number of something but you don’t know how many. For example
1
2
3
4
[general]
things.thing1 = onething
things.thing2 = anotherthing
things.thing3 = onemore
After parsing this configuration file you could print all three things using a foreach loop:
1
2
3
4
foreach ($config->things as $t)
{
echo $t.'<br>';
}
php
zend_framework
]