3.4. Zend_Config_Xml

3.4.1. Introduction

Zend_Config_Xml enables developers to store configuration data in a simple XML format and read them via object properties through Zend_Config.

例 3.4. Configuration Data Stored in XML Files

This example illustrates a basic use of Zend_Config_Xml for loading configuration data from an XML file. Suppose we have the following configuration data in /path/to/config.xml:

<?xml version="1.0"?>
<config>
    <production>
        <webhost>www.example.com</webhost>
        <database>
            <type>pdo_mysql</type>
            <host>db.example.com</host>
            <username>dbuser</username>
            <password>secret</password>
            <name>dbname</name>
        </database>
    </production>
    <staging extends="production">
        <database>
            <host>dev.example.com</host>
            <username>devuser</username>
            <password>devsecret</password>
        </database>
    </staging>
</config>    

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

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

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

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

[注意] 注意
Configuration data values from Zend_Config_Xml are always treated as strings.