Introduction to Zend Framework

 Learning Zend Framework

appendix

 Zend Framework Reference


  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Update 2011-11-16 - Revision 24438 - Version ZF 1.11.x

    Chapter 21. Zend_Config

    21.1. Introduction

    Zend_Config is designed to simplify the access to, and the use of, configuration data within applications. It provides a nested object property based user interface for accessing this 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 21.1. Using Zend_Config

    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:

    // 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
    $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:

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

    Example 21.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;

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...