Introduction

Zend\Config is designed to simplify access to 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 that read and write configuration data stored in .ini, JSON, YAML and XML files.

Using Zend\Config\Config with a Reader Class

Normally, it is expected that users would use one of the reader classes to read a configuration file, but if configuration data are available in a PHP array, one may simply pass the data to Zend\Config\Config‘s constructor in order to utilize a simple object-oriented interface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// An array of configuration data is given
$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 using the configuration data
$config = new Zend\Config\Config($configArray);

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

As illustrated in the example above, Zend\Config\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\Config also has get() method that returns the supplied value if the data element doesn’t exist in the configuration array. For example:

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

Using Zend\Config\Config with a PHP Configuration File

It is often desirable to use a purely PHP-based configuration file. The following code illustrates how easily this can be accomplished:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// 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'
        )
    )
);
1
2
3
4
5
// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');

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

Project Versions

Table Of Contents

Previous topic

CAPTCHA Adapters

Next topic

Theory of Operation

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Introduction on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.