Zend_Config_Ini.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.config.adapters.ini">
  4. <title>Zend_Config_Ini</title>
  5. <para>
  6. <classname>Zend_Config_Ini</classname> enables developers to store configuration data in a familiar INI format and read
  7. them in the application by using nested object property syntax. The INI format is specialized to provide both
  8. the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections.
  9. Configuration data hierarchies are supported by separating the keys with the dot or period character
  10. (<code>.</code>). A section may extend or inherit from another section by following the section name with a
  11. colon character (<code>:</code>) and the name of the section from which data are to be inherited.
  12. </para>
  13. <note>
  14. <title>Parsing the INI File</title>
  15. <para>
  16. <classname>Zend_Config_Ini</classname> utilizes the
  17. <ulink url="http://php.net/parse_ini_file"><code>parse_ini_file()</code></ulink>
  18. PHP function. Please review this documentation to be aware of its specific behaviors,
  19. which propagate to <classname>Zend_Config_Ini</classname>, such as how the special values of
  20. <code>true</code>, <code>false</code>, <code>yes</code>, <code>no</code>,
  21. and <code>null</code> are handled.
  22. </para>
  23. </note>
  24. <note>
  25. <title>Key Separator</title>
  26. <para>
  27. By default, the key separator character is the period character (<code>.</code>). This can be changed,
  28. however, by changing the <code>$options</code> key <code>'nestSeparator'</code> when constructing the
  29. <classname>Zend_Config_Ini</classname> object. For example:
  30. <programlisting role="php"><![CDATA[
  31. $options['nestSeparator'] = ':';
  32. $config = new Zend_Config_Ini('/path/to/config.ini',
  33. 'staging',
  34. $options);
  35. ]]></programlisting>
  36. </para>
  37. </note>
  38. <example id="zend.config.adapters.ini.example.using">
  39. <title>Using Zend_Config_Ini</title>
  40. <para>
  41. This example illustrates a basic use of <classname>Zend_Config_Ini</classname> for loading configuration data from an
  42. INI file. In this example there are configuration data for both a production system and for a staging
  43. system. Because the staging system configuration data are very similar to those for production, the staging
  44. section inherits from the production section. In this case, the decision is arbitrary and could have been
  45. written conversely, with the production section inheriting from the staging section, though this may not be
  46. the case for more complex situations. Suppose, then, that the following configuration data are contained in
  47. <code>/path/to/config.ini</code>:
  48. </para>
  49. <programlisting role="ini"><![CDATA[
  50. ; Production site configuration data
  51. [production]
  52. webhost = www.example.com
  53. database.adapter = pdo_mysql
  54. database.params.host = db.example.com
  55. database.params.username = dbuser
  56. database.params.password = secret
  57. database.params.dbname = dbname
  58. ; Staging site configuration data inherits from production and
  59. ; overrides values as necessary
  60. [staging : production]
  61. database.params.host = dev.example.com
  62. database.params.username = devuser
  63. database.params.password = devsecret
  64. ]]></programlisting>
  65. <para>
  66. Next, assume that the application developer needs the staging configuration data from the INI file. It is a
  67. simple matter to load these data by specifying the INI file and the staging section:
  68. </para>
  69. <programlisting role="php"><![CDATA[
  70. $config = new Zend_Config_Ini('/path/to/config.ini', 'staging');
  71. echo $config->database->params->host; // prints "dev.example.com"
  72. echo $config->database->params->dbname; // prints "dbname"
  73. ]]></programlisting>
  74. </example>
  75. <note>
  76. <table id="zend.config.adapters.ini.table">
  77. <title>Zend_Config_Ini Constructor Parameters</title>
  78. <tgroup cols="2">
  79. <thead>
  80. <row>
  81. <entry>Parameter</entry>
  82. <entry>Notes</entry>
  83. </row>
  84. </thead>
  85. <tbody>
  86. <row>
  87. <entry><code>$filename</code></entry>
  88. <entry>The INI file to load.</entry>
  89. </row>
  90. <row>
  91. <entry><code>$section</code></entry>
  92. <entry>The [section] within the ini file that is to be loaded. Setting
  93. this parameter to null will load all sections. Alternatively, an
  94. array of section names may be supplied to load multiple sections.</entry>
  95. </row>
  96. <row>
  97. <entry><code>$options = false</code></entry>
  98. <entry>Options array. The following keys are supported:
  99. <itemizedlist>
  100. <listitem><para><emphasis>allowModifications</emphasis>: Set to true to allow subsequent modification of loaded file. Defaults to false</para></listitem>
  101. <listitem><para><emphasis>nestSeparator</emphasis>: Set to the character to be used as the nest separator. Defaults to "."</para></listitem>
  102. </itemizedlist>
  103. </entry>
  104. </row>
  105. </tbody>
  106. </tgroup>
  107. </table>
  108. </note>
  109. </sect1>
  110. <!--
  111. vim:se ts=4 sw=4 et:
  112. -->