Zend_Config_Yaml - Zend_Config



» YAML is a recursive acronym meaning "YAML Ain't Markup Language", and is intended as a "human friendly data serialization standard for all programming languages." It is often used for application configuration.

Zend_Config_Yaml is a lightweight Zend_Config extension. It includes a parser capable of recognizing most common YAML syntax used for purposes of configuration, and allows specifying other parsers should you want more complex syntax (e.g., ext/syck, spyc, sfYaml, etc.).

Quick Start

The following is a YAML version of a standard application configuration.

  1. production:
  2.   phpSettings:
  3.     display_startup_errors: false
  4.     display_errors: false
  5.   includePaths:
  6.     library: APPLICATION_PATH/../library
  7.   bootstrap:
  8.     path: APPLICATION_PATH/Bootstrap.php
  9.     class: "Bootstrap"
  10.   appnamespace: "Application"
  11.   resources:
  12.     frontController:
  13.       controllerDirectory: APPLICATION_PATH/controllers
  14.       moduleDirectory: APPLICATION_PATH/modules
  15.       params:
  16.         displayExceptions: false
  17.     modules:
  18.     db:
  19.       adapter: "pdo_sqlite"
  20.       params:
  21.         dbname: APPLICATION_PATH/../data/db/application.db
  22.     layout:
  23.       layoutPath: APPLICATION_PATH/layouts/scripts/
  25. staging:
  26.   _extends: production
  28. testing:
  29.   _extends: production
  30.   phpSettings:
  31.     display_startup_errors: true
  32.     display_errors: true
  34. development:
  35.   _extends: production
  36.   phpSettings:
  37.     display_startup_errors: true
  38.     display_errors: true
  39.   resources:
  40.     frontController:
  41.       params:
  42.         displayExceptions: true

To utilize it, you simply instantiate Zend_Config_Yaml, pointing it to the location of this file and indicating the section of the file to load. By default, constant names found in values will be substituted with their appropriate values.

  1. $config = new Zend_Config_Yaml(
  2.     APPLICATION_PATH . '/configs/application.yaml',
  4. );

Once instantiated, you use it as you would any other configuration object.

  1. $db = Zend_Db::factory($config->resources->db);

Configuration Options

The following options may be passed as keys to the third, $options argument of the constructor.

Zend_Config_Yaml Options


The default behavior of Zend_Config is to mark the object as immutable once loaded. Passing this flag with a boolean true will enable modifications to the object.


By default, any time a section extends another, Zend_Config will merge the section with the section it extends. Speciying a boolean true value to this option will disable this feature, giving you only the configuration defined explicitly in that section.


By default, Zend_Config_Yaml will replace constant names found in values with the defined constant value. You map pass a boolean true to this option to disable this functionality.


By default, Zend_Config_Yaml uses a built in decoder, Zend_Config_Yaml::decode(), to parse and process YAML files. You may specify an alternate callback to use in place of the built-in one using this option.

Available Methods

__construct ( $yaml, $section = null, $options = false )

Constructor. $yaml should refer to a valid filesystem location containing a YAML configuration file. $section, if specified, indicates a specific section of the configuration file to use. $options is discussed in the options section.

decode ( $yaml )

Parses a YAML string into a PHP array.

setIgnoreConstants ( $flag )

This static function may be used to globally override the default settings for how constants found in YAML strings are handled. By default, constant names are replaced with the appropriate constant values; passing a boolean true value to this method will override that behavior. (You can override it per-instance via the ignore_constants option as well.)

ignoreConstants ( )

This static method gives you the current setting for the ignore_constants flag.


Example #1 Using Zend_Config_Yaml with sfYaml

As noted in the options section, Zend_Config_Yaml allows you to specify an alternate YAML parser at instantiation.

» sfYaml is a » Symfony component that implements a complete YAML parser in PHP, and includes a number of additional features including the ability to parse PHP expressions embedded in the YAML. In this example, we use the sfYaml::load() method as our YAML decoder callback. (Note: this assumes that the sfYaml class is either already loaded or available via autoloading.)

  1. $config = new Zend_Config_Yaml(
  2.     APPLICATION_PATH . '/configs/application.yaml',
  4.     array('yaml_decoder' => array('sfYaml', 'load'))
  5. );


© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.