Zend_Config_Ini

Zend_Config_Json

Overview

» JSON is an acronym for "JavaScript Object Notation"; while compatible with JavaScript, it is also intended as a general-purpose, cross-language data interchange format. Zend_Config_Json is a lightweight Zend_Config extension using JSON as its serialization format.

Quick Start

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

  1. {
  2.     "production":{
  3.         "phpSettings":{
  4.             "display_startup_errors": false,
  5.             "display_errors": false
  6.         },
  7.         "includePaths":{
  8.             "library": "APPLICATION_PATH/../library"
  9.         },
  10.         "bootstrap":{
  11.             "path": "APPLICATION_PATH/Bootstrap.php",
  12.             "class": "Bootstrap"
  13.         },
  14.         "appnamespace": "Application",,
  15.         "resources":{
  16.             "frontController":{
  17.                 "controllerDirectory": "APPLICATION_PATH/controllers",
  18.                 "moduleDirectory": "APPLICATION_PATH/modules",
  19.                 "params":{
  20.                     "displayExceptions": false
  21.                 }
  22.             },
  23.             "modules":[],
  24.             "db":{
  25.                 "adapter": "pdo_sqlite",
  26.                 "params":{
  27.                     "dbname": "APPLICATION_PATH/../data/db/application.db"
  28.                 }
  29.             },
  30.             "layout":{
  31.                 "layoutPath": "APPLICATION_PATH/layouts/scripts/"
  32.             }
  33.         }
  34.     },
  35.     "staging":{
  36.         "_extends": "production"
  37.     },
  38.     "testing":{
  39.         "_extends": "production",
  40.         "phpSettings":{
  41.             "display_startup_errors": true,
  42.             "display_errors": true
  43.         },
  44.     },
  45.     "development":{
  46.         "_extends": "production",
  47.         "phpSettings":{
  48.             "display_startup_errors": true,
  49.             "display_errors": true
  50.         },
  51.         "resources":{
  52.             "frontController":{
  53.                 "params":{
  54.                     "displayExceptions": true
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }

To utilize it, you simply instantiate Zend_Config_Json, 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_Json(
  2.     APPLICATION_PATH . '/configs/application.json',
  3.     APPLICATION_ENV
  4. );

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

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

Use Constants With Care

JSON has a strict structure with regards to data types. As such, you need to ensure that your constants are use correctly. For constants that have string values, put your constant values in double quotes (""). For non-string values, you can omit the quotes -- but be absolutely certain that they are not returning strings, as otherwise you will encounter parser errors with your configuration file. When in doubt, enclose the contant in double quotes.

Configuration Options

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

Zend_Config_Json Options

allow_modifications/allowModifications

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.

skip_extends/skipExtends

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.

ignore_constants

By default, Zend_Config_Json 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.

Please note that ignoring constants can potentially lead to parse errors, particularly if you are using constants for integer, float, or boolean values. The safest practice is to enclose constants within quotes.

Available Methods

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

Constructor. $json should be either a valid JSON string, or refer to a valid filesystem location containing a JSON configuration file. $section, if specified, indicates a specific section of the configuration file to use. $options is discussed in the options section.

setIgnoreConstants ( $flag )

This static function may be used to globally override the default settings for how constants found in JSON 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.


Zend_Config_Ini