3.3. Zend_Config_Ini

3.3.1. Introduction

Zend_Config_Ini enables developers to store configuration data in a familiar INI format and read them via object properties through Zend_Config.

例 3.3. Configuration Data Stored in INI Files

This example illustrates a basic use of Zend_Config_Ini for loading configuration data from an INI file. Suppose we have the following configuration data 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]
extends = production
database.host     = dev.example.com
database.username = devuser
database.password = devsecret
                

[注意] 注意
Notice that the syntax of the INI file resembles that expected by the parse_ini_file() PHP function, which Zend_Config_Ini utilizes. Please review this documentation to be aware of the specific behavior of Zend_Config_Ini, such as how the special values of true, false, yes, no, and null are translated.

Inheritance is supported in the INI syntax by using the keyword extends. The value of the extends key should be the name of the section from which the extending section is to inherit. The configuration data are then loaded from the INI file:

<?php
require_once 'Zend/Config.php';
require_once 'Zend/Config/Ini.php';

$config = new Zend_Config(Zend_Config_Ini::load('/path/to/config.ini', 'staging'));

echo $config->database->host; // prints "dev.example.com"
?>