1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.config.introduction">
4 <title>Introduction</title>
7 <classname>Zend_Config</classname> is designed to simplify the access to, and the use of,
8 configuration data within applications. It provides a nested object property based user
9 interface for accessing this configuration data within application code. The configuration
10 data may come from a variety of media supporting hierarchical data storage. Currently
11 <classname>Zend_Config</classname> provides adapters for configuration data that are stored
12 in text files with <link
13 linkend="zend.config.adapters.ini"><classname>Zend_Config_Ini</classname></link> and
14 <link linkend="zend.config.adapters.xml"><classname>Zend_Config_Xml</classname></link>.
17 <example id="zend.config.introduction.example.using">
18 <title>Using Zend_Config</title>
21 Normally it is expected that users would use one of the adapter classes such as <link
22 linkend="zend.config.adapters.ini"><classname>Zend_Config_Ini</classname></link> or
23 <link linkend="zend.config.adapters.xml"><classname>Zend_Config_Xml</classname></link>,
24 but if configuration data are available in a <acronym>PHP</acronym> array, one may
25 simply pass the data to the <classname>Zend_Config</classname> constructor in order to
26 utilize a simple object-oriented interface:
29 <programlisting language="php"><![CDATA[
30 // Given an array of configuration data
32 'webhost' => 'www.example.com',
34 'adapter' => 'pdo_mysql',
36 'host' => 'db.example.com',
37 'username' => 'dbuser',
38 'password' => 'secret',
39 'dbname' => 'mydatabase'
44 // Create the object-oriented wrapper upon the configuration data
45 $config = new Zend_Config($configArray);
47 // Print a configuration datum (results in 'www.example.com')
48 echo $config->webhost;
50 // Use the configuration data to connect to the database
51 $db = Zend_Db::factory($config->database->adapter,
52 $config->database->params->toArray());
54 // Alternative usage: simply pass the Zend_Config object.
55 // The Zend_Db factory knows how to interpret it.
56 $db = Zend_Db::factory($config->database);
61 As illustrated in the example above, <classname>Zend_Config</classname> provides nested
62 object property syntax to access configuration data passed to its constructor.
66 Along with the object oriented access to the data values,
67 <classname>Zend_Config</classname> also has <methodname>get()</methodname> which will
68 return the supplied default value if the data element doesn't exist. For example:
71 <programlisting language="php"><![CDATA[
72 $host = $config->database->get('host', 'localhost');
75 <example id="zend.config.introduction.example.file.php">
76 <title>Using Zend_Config with a PHP Configuration File</title>
79 It is often desirable to use a pure <acronym>PHP</acronym>-based configuration file.
80 The following code illustrates how easily this can be accomplished:
83 <programlisting language="php"><![CDATA[
86 'webhost' => 'www.example.com',
88 'adapter' => 'pdo_mysql',
90 'host' => 'db.example.com',
91 'username' => 'dbuser',
92 'password' => 'secret',
93 'dbname' => 'mydatabase'
99 <programlisting language="php"><![CDATA[
100 // Configuration consumption
101 $config = new Zend_Config(require 'config.php');
103 // Print a configuration datum (results in 'www.example.com')
104 echo $config->webhost;