Zend_Config_Ini
enables developers to store configuration data in a familiar INI format and read
them in the application by using nested object property syntax. The INI format is specialized to provide both
the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections.
Configuration data hierarchies are supported by separating the keys with the dot or period character
(.
). A section may extend or inherit from another section by following the section name with a
colon character (:
) and the name of the section from which data are to be inherited.
parse_ini_file | |
---|---|
|
Key Separator | |
---|---|
By default, the key separator character is the period character ( <?php require_once 'Zend/Config/Ini.php'; $config['nestSeparator'] = ':'; $config = new Zend_Config_Ini('/path/to/config.ini', 'staging', $config);
|
例 5.2. Using Zend_Config_Ini
This example illustrates a basic use of Zend_Config_Ini
for loading configuration data from an
INI file. In this example there are configuration data for both a production system and for a staging
system. Because the staging system configuration data are very similar to those for production, the staging
section inherits from the production section. In this case, the decision is arbitrary and could have been
written conversely, with the production section inheriting from the staging section, though this may not be
the case for more complex situations. Suppose, then, that the following configuration data are contained in
/path/to/config.ini
:
; Production site configuration data [production] webhost = www.example.com database.type = pdo_mysql database.host = db.example.com database.username = dbuser database.password = secret database.name = dbname ; Staging site configuration data inherits from production and ; overrides values as necessary [staging : production] database.host = dev.example.com database.username = devuser database.password = devsecret
Next, assume that the application developer needs the staging configuration data from the INI file. It is a simple matter to load these data by specifying the INI file and the staging section:
<?php require_once 'Zend/Config/Ini.php'; $config = new Zend_Config_Ini('/path/to/config.ini', 'staging'); echo $config->database->host; // prints "dev.example.com" echo $config->database->name; // prints "dbname"