Zend_Config-Introduction.xml 4.0 KB

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