第 7 章 Zend_Config

目录

7.1. 简介
7.2. 操作理论
7.3. Zend_Config_Ini
7.4. Zend_Config_Xml

7.1. 简介

Zend_Config被设计在应用程序中简化访问和使用配置数据。它为在应用程序代码中访问这样的配置数据提供了一个基于用户接口的嵌入式对象属性。配置数据可能来自于各种支持等级结构数据存储的媒体。当前Zend_Config为存储在Zend_Config_IniZend_Config_Xml的文本文件中的配置数据提供了适配器。

例 7.1. 使用 Zend_Config 本身

通常用户可以使用象Zend_Config_IniZend_Config_Xml适配器类的其中之一,但如果配置数据在PHP数组里可用,为了使用一个简单的面向对象的接口,可以简单地传递数据到Zend_Config构造器:

// 给出一个配置数据的数组
$configArray = array(
    'webhost'  => 'www.example.com',
    'database' => array(
        'adapter' => 'pdo_mysql',
        'params'  => array(
            'host'     => 'db.example.com',
            'username' => 'dbuser',
            'password' => 'secret',
            'dbname'   => 'mydatabase'
        )
    )
);

// 基于配置数据创建面向对象的 wrapper 
$config = new Zend_Config($configArray);

// 输出配置数据 (结果在'www.example.com'中)
echo $config->webhost;

// 使用配置数据来连接数据库
$db = Zend_Db::factory($config->database->adapter,
                       $config->database->params->toArray());

// 另外的用法:简单地传递 Zend_Config 对象。
// Zend_Db factory 知道如何翻译它。
$db = Zend_Db::factory($config->database);

        

如上例所示,Zend_Config 提供嵌套的对象属性语句来访问传递给它的构造器的配置数据。

连同面向对象访问数据值,Zend_Config也有get()方法,如果数据元素不存在,它将返回提供的缺省值,例如:

$host = $config->database->get('host', 'localhost');

    

例 7.2. Using Zend_Config with a PHP Configuration File

It is often desirable to use a pure PHP-based configuration file. The following code illustrates how easily this can be accomplished:

// config.php
return array(
    'webhost'  => 'www.example.com',
    'database' => array(
        'adapter' => 'pdo_mysql',
        'params'  => array(
            'host'     => 'db.example.com',
            'username' => 'dbuser',
            'password' => 'secret',
            'dbname'   => 'mydatabase'
        )
    )
);

        
// Configuration consumption
$config = new Zend_Config(require 'config.php');

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