Zend_Config-Introduction.xml 3.9 KB

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