Zend_Config_Array enables developers to have configuration data stored in multidimensional
associative PHP arrays and accessed as object properties through Zend_Config.
Example 3.2. Configuration Data Stored in PHP Arrays
This example illustrates a basic use of Zend_Config_Array for loading configuration
data from PHP arrays. First, we need to create PHP arrays having the configuration data:
<?php
// Production site configuration data
$config['production'] = array(
'webhost' => 'www.example.com',
'database' => array(
'type' => 'pdo_mysql',
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'name' => 'dbname'
)
);
// Staging site configuration data inherits from production and
// overrides values as necessary
$config['staging'] = $config['production'];
$config['staging']['webhost'] = 'dev.example.com';
$config['staging']['database']['host'] = 'dev.example.com';
$config['staging']['database']['username'] = 'devuser';
$config['staging']['database']['password'] = 'devsecret';
?>
![]() |
Note |
|---|---|
The configuration data array must be named $config in the loaded PHP script.
|
Inheritance is supported in the example above with an assignment of production section
values to the staging section. Now it is a simple matter to load the configuration
data with Zend_Config_Array:
<?php
require_once 'Zend/Config.php';
require_once 'Zend/Config/Array.php';
$config = new Zend_Config(Zend_Config_Array::load('/path/to/config.php', 'staging'));
echo $config->database->host; // prints "dev.example.com"
?>
It is noteworthy that because the configuration data are directly added to PHP arrays, each
configuration data value has the same data type as given in the PHP script containing the
$config array.
<?php $config['staging']['debug'] = 'false'; // value has string type $config['staging']['console'] = false; // value of boolean type $config['staging']['timeout'] = 30; // value of integer type ?>