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

Chapter 5. Zend_Config

5.1. Introduction

Zend_Config is designed to simplify access to and use of configuration data within applications. It provides a nested object property based user interface for accessing such configuration data within application code. The configuration data may come from a variety of media supporting hierarchical data storage. Currently Zend_Config provides adapters for configuration data that are stored in text files with Zend_Config_Ini and Zend_Config_Xml.

Example 5.1. Using Zend_Config Per Se

Normally it is expected that users would use one of the adapter classes such as Zend_Config_Ini or Zend_Config_Xml, but if configuration data are available in a PHP array, one may simply pass the data to the Zend_Config constructor in order to utilize a simple object-oriented interface:

<?php
// Given an array of configuration data
$configArray = array(
    'webhost'  => 'www.example.com',
    'database' => array(
        'adapter' => 'pdo_mysql',
        'params'  => array(
            'host'     => 'db.example.com',
            'username' => 'dbuser',
            'password' => 'secret',
            'dbname'   => 'mydatabase'
        )
    )
);

// Create the object-oriented wrapper upon the configuration data
require_once 'Zend/Config.php';
$config = new Zend_Config($configArray);

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

// Use the configuration data to connect to the database
$db = Zend_Db::factory($config->database->adapter,
                       $config->database->params->toArray());

// Alternative usage: simply pass the Zend_Config object.
// The Zend_Db factory knows how to interpret it.
$db = Zend_Db::factory($config->database);
        

As illustrated in the example above, Zend_Config provides nested object property syntax to access configuration data passed to its constructor.

Along with the object oriented access to the data values, Zend_Config also has get() which will return the supplied default value if the data element doesn't exist. For example:

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

Example 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