upgrade zend (#1559)
[openemr.git] / vendor / zendframework / zend-config / doc / book / intro.md
blob9d33d72c81feec6ffeea8b42ea2290a9b8c01c40
1 # Introduction
3 zend-config is designed to simplify access to configuration data within applications. It provides
4 a nested object, property-based user interface for accessing this configuration data within
5 application code. The configuration data may come from a variety of formats supporting hierarchical
6 data storage. Currently, zend-config provides adapters that read and write configuration data
7 stored in INI, JSON, YAML, and XML files.
9 ## Using Reader Classes
11 Normally, users will use one of the [reader classes](reader.md) to read a
12 configuration file, but if configuration data is available in a PHP array, one
13 may simply pass the data to `Zend\Config\Config`'s constructor in order to
14 utilize a simple object-oriented interface:
16 ```php
17 // An array of configuration data is given
18 $configArray = [
19     'webhost'  => 'www.example.com',
20     'database' => [
21         'adapter' => 'pdo_mysql',
22         'params'  => [
23             'host'     => 'db.example.com',
24             'username' => 'dbuser',
25             'password' => 'secret',
26             'dbname'   => 'mydatabase',
27         ],
28     ],
31 // Create the object-oriented wrapper using the configuration data
32 $config = new Zend\Config\Config($configArray);
34 // Print a configuration datum (results in 'www.example.com')
35 echo $config->webhost;
36 ```
38 As illustrated in the example above, `Zend\Config\Config` provides nested object
39 property syntax to access configuration data passed to its constructor.
41 Along with the object-oriented access to the data values, `Zend\Config\Config`
42 also has a `get()` method that accepts a default value to return if the data
43 element requested doesn't exist in the configuration array. For example:
45 ```php
46 $host = $config->database->get('host', 'localhost');
47 ```
49 ## Using PHP Configuration Files
51 PHP-basd configuration files are often recommended due to the speed with which
52 they are parsed, and the fact that they can be cached by opcode caches.
54 The following code illustrates how to use PHP configuration files:
56 ```php
57 // config.php
58 return [
59     'webhost'  => 'www.example.com',
60     'database' => array(
61         'adapter' => 'pdo_mysql',
62         'params'  => [
63             'host'     => 'db.example.com',
64             'username' => 'dbuser',
65             'password' => 'secret',
66             'dbname'   => 'mydatabase',
67         ],
68     ],
70 ```
72 ```php
73 // Consumes the configuration array
74 $config = new Zend\Config\Config(include 'config.php');
76 // Print a configuration datum (results in 'www.example.com')
77 echo $config->webhost;
78 ```