Available Languages
Manual for Zend Framework 1.0.3
Zend_Config_Xml 让开发者能够存储配置数据到一个简单XML格式并通过嵌入对象属性语法来读取。XML文件的根元素(root element)不相关并可以任意命名。顶级的XML元素和配置数据的节相对应。XML格式通过嵌入XML元素到节一级元素(section-level elements)的下面来支持等级结构组织。叶一级(leaf-level)的XML元素和配置数据的值相对应。节继承通过一个特殊的XML属性名为extends来支持,与之相对应的这个属性的值通过扩展节(extending section)来继承。
![]() |
返回类型 |
|---|---|
读入 |
例 5.4. 使用Zend_Config_Xml
这个例子示例了从INI文件加载配置数据的Zend_Config_Xml的基本用法。在这个例子中有生产系统(production system)和开发系统(staging system)的配置数据。因为开发系统配置数据和生产系统的配置数据类似,所以开发系统的节从生产系统的节继承。在这个案例中,结果(decision)是任意的并且它可以反过来做,即生产系统节从开发系统节继承,尽管这不可能用于更复杂的情形。接着,假定下面的配置数据包含在/path/to/config.xml中:
<?xml version="1.0"?>
<configdata>
<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>
</configdata>
接着,假定开发者需要从XML文件取开发配置数据。这非常简单,只要指定XML文件和开发系统节就可以加载这些数据了:
<?php
require_once 'Zend/Config/Xml.php';
$config = new Zend_Config_Xml('/path/to/config.xml', 'staging');
echo $config->database->host; // 输出 "dev.example.com"
echo $config->database->name; // 输出 "dbname"
Welcome!
Welcome to ZFResource - The Resource for the Zend Framework.
As you see, the website is in still in progress. Many features ( like Code Directory, Code Samples, User written Tutorials, Examples and News,...) will be online in near future.
At the moment, you can search and browse the manual in your language of
choice. Don't forget to come back later to benefit from the new features.
Help Wanted:
We are still searching for people want to help building this site. If you want to see this website in your language or you have any suggestions for this site please send us an email
![[注意]](images/note.png)
