Chapter 5. Zend_Config

Table of Contents

5.1. Introduction
5.2. Theory of Operation
5.3. Zend_Config_Ini
5.4. Zend_Config_Xml

5.1. Introduction

Zend_Config is designed to simplify access to and use of configuration data within applications. It provides a nested object property based user interface for accessing such configuration data within application code. The configuration data may come from a variety of media supporting hierarchical data storage. Currently Zend_Config provides adapters for configuration data that are stored in text files with Zend_Config_Ini and Zend_Config_Xml.

Example 5.1. Using Zend_Config Per Se

Normally it is expected that users would use one of the adapter classes such as Zend_Config_Ini or Zend_Config_Xml, but if configuration data are available in a PHP array, one may simply pass the data to the Zend_Config constructor in order to utilize a simple object-oriented interface:

<?php
// Given an array of configuration data
$configArray = array(
    'webhost' => 'www.example.com',
    'database' => array(
        'type'     => 'pdo_mysql',
        'host'     => 'db.example.com',
        'username' => 'dbuser',
        'password' => 'secret',
        'name'     => 'dbname'
    )
);

// Create the object-oriented wrapper upon the configuration data
require_once 'Zend/Config.php';
$config = new Zend_Config($configArray);

// Print a configuration datum (results in 'www.example.com')
echo $config->webhost;

// Use the configuration data to connect to the database
$myApplicationObject->databaseConnect($config->database->type,
                                      $config->database->host,
                                      $config->database->username,
                                      $config->database->password,
                                      $config->database->name);

As illustrated in the example above, Zend_Config provides nested object property syntax to access configuration data passed to its constructor.