Documentation

Zend\Config

Zend\Config\Reader gives you the ability to read a config file. It works with concrete implementations for
different file format. The Zend\Config\Reader is only an interface, that define the two methods fromFile()
and fromString(). The concrete implementations of this interface are:

Zend\Config\Reader\Ini

Zend\Config\Reader\Xml

Zend\Config\Reader\Json

Zend\Config\Reader\Yaml

Zend\Config\Reader\JavaProperties

The fromFile() and fromString() return a PHP array contains the data of the configuration file.

Note

Differences from ZF1

The Zend\Config\Reader component no longer supports the following features:

Zend\Config\Reader\Ini enables developers to store configuration data in a familiar INI format and read them
in the application by using an array syntax.

Zend\Config\Reader\Ini utilizes the parse_ini_file()PHP function. Please review this documentation to be
aware of its specific behaviors, which propagate to Zend\Config\Reader\Ini, such as how the special values of
“TRUE”, “FALSE”, “yes”, “no”, and “NULL” are handled.

Note

Key Separator

By default, the key separator character is the period character (“.”). This can be changed, however, using
the setNestSeparator() method. For example:

1
2

$reader=newZend\Config\Reader\Ini();$reader->setNestSeparator('-');

The following example illustrates a basic use of Zend\Config\Reader\Ini for loading configuration data from an
INI file. In this example there are configuration data for both a production system and for a staging system.
Suppose we have the following INI configuration file:

Zend\Config\Reader\Xml enables developers to read configuration data in a familiar XML format and read them
in the application by using an array syntax. The root element of the XML file or string is irrelevant and may be
named arbitrarily.

The following example illustrates a basic use of Zend\Config\Reader\Xml for loading configuration data from an
XML file. Suppose we have the following XML configuration file:

Zend\Config\Reader\Xml utilizes the XMLReaderPHP class. Please review this documentation to be aware of
its specific behaviors, which propagate to Zend\Config\Reader\Xml.

Using Zend\Config\Reader\Xml we can include the content of XML files in a specific XML element. This is
provided using the standard function XInclude of XML. To use this function you have to add the namespace
xmlns:xi="http://www.w3.org/2001/XInclude" to the XML file. Suppose we have an XML files that contains only the
database configuration:

Using Zend\Config\Reader\Json we can include the content of a JSON file in a specific JSON section or element.
This is provided using the special syntax @include. Suppose we have a JSON file that contains only the database
configuration:

Zend\Config\Reader\Yaml enables developers to read configuration data in a YAML format and read them in the
application by using an array syntax. In order to use the YAML reader we need to pass a callback to an external PHP
library or use the Yaml PECL extension.

The following example illustrates a basic use of Zend\Config\Reader\Yaml that use the Yaml PECL extension.
Suppose we have the following YAML configuration file:

You can also instantiate the Zend\Config\Reader\Yaml without any parameter and specify the YAML reader in a
second moment using the setYamlDecoder() method.

Using Zend\Config\ReaderYaml we can include the content of a YAML file in a specific YAML section or element.
This is provided using the special syntax @include. Suppose we have a YAML file that contains only the database
configuration:

Zend\Config\Reader\JavaProperties enables developers to read configuration data in a familiar JavaProperties format and read them
in the application by using an array syntax.

The following example illustrates a basic use of Zend\Config\Reader\JavaProperties for loading configuration data from an
JavaProperties file. Suppose we have the following JavaProperties configuration file: