Coding Samples, Tutorials, Code Snippets, Articles, How-To`s - From Beginners to Expert. Stay tuned.
Share your knowledge. Regardless what experience you have made with the Zend Framework - Let all benefit.

Search Manual:

Please Sign in or Register

Available Languages

 


Manual for Zend Framework 1.0.3

第 5 章 Zend_Config

5.1. 简介

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

例 5.1. 使用 Zend_Config 本身

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

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

// 基于配置数据创建面向对象的 wrapper 
require_once 'Zend/Config.php';
$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()方法,如果数据元素不存在,它将返回提供的缺省值,例如:

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

例 5.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:

<?php
// 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'
        )
    )
);
<?php
// Configuration consumption
require_once 'Zend/Config.php';
$config = new Zend_Config(require 'config.php');

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

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