第 3 章 Zend_Config
上一页 |  | 下一页

## 第 3 章 Zend_Config

## 3.1. Introduction

`Zend_Config` is designed to simplify the use of configuration data for web applications. It provides a property-based interface for reading configuration data from a variety of media supporting hierarchical data storage. Currently `Zend_Config` provides helper classes for configuration data stored in text files via `Zend_Config_Array`, `Zend_Config_Ini`, and `Zend_Config_Xml`.

`Zend_Config` supports a single inheritance model that enables configuration data to be inherited from one section of configuration data into another. Though `Zend_Config` supports inheritance trees of arbitrary height, a configuration data section may only inherit from one parent section.

Internally `Zend_Config` implements the Countable and Iterator interfaces in order to facilitate easy access to configuration data. The data are made accessible to `Zend_Config` through an associative array, which may be multidimensional. A helper class such as `Zend_Config_Ini` typically provides this array to `Zend_Config`, but the interface does not require using this convention.

| 注意
Though `Zend_Config` allows in-memory modification of loaded configuration data, it is not designed to facilitate saving configuration data to specific storage media. Tools for creating and modifying configuration data for specific storage media are currently outside of the Zend Framework scope. Third-party open source solutions are often available for the purpose of creating and modifying configuration data for various storage media.

例 3.1. Loading Configuration Data from an INI File

In this example configuration data for a production environment are loaded from an INI file using `Zend_Config_Ini`:

    <?php
    require_once 'Zend/Config.php';
    require_once 'Zend/Config/Ini.php';
    $config = new Zend_Config(Zend_Config_Ini::load('/path/to/config.ini', 'production'));
    ?>

Now the configuration data are available from the `$config` object's properties. Suppose the configuration data include database connection parameters and that the data are stored in the INI file as:

    [production]
    database.type     = pdo_mysql
    database.host     = db.example.com
    database.username = dbuser
    database.password = secret
    database.name     = dbname

The application might create a connection to the database with:

    <?php
    $myApplicationObject->databaseConnect($config->database->type,
                                          $config->database->host,
                                          $config->database->username,
                                          $config->database->password,
                                          $config->database->name);
    ?>

As shown above, the configuration data are accessed with the object property syntax.

We can also extend `Zend_Config` easily for various purposes. Here an extending class introduces a public `dump()` method for quickly printing loaded configuration data:

    <?php
    class MyConfig extends Zend_Config
    {
        protected $_indent;

        public function dump()
        {
            $this->_indent = 0;
            echo "<pre>\n";
            $this->_dumpRecursor($this);
            echo "</pre>";
        }

        protected function _dumpRecursor($config)
        {
            foreach ($config as $key => $value) {
                echo str_repeat("    ", $this->_indent) . "$key =>";
                if ($value instanceof Zend_Config) {
                    echo "\n";
                    $this->_indent++;
                    $this->_dumpRecursor($value);
                    $this->_indent--;
                } else {
                    echo " $value\n";
                }
            }
        }
    }

    $config = new MyConfig(Zend_Config_Ini::load('/path/to/config.ini', 'production'));

    $config->dump();
    ?>

For the configuration data in the example above, this would print:

    <pre>
    database =>
        type => pdo_mysql
        host => db.example.com
        username => dbuser
        password => secret
        name => dbname
    </pre>

上一页 |  | 下一页
第 2 章 Zend_Cache | 起始页 | 3.2. Zend_Config_Array