PHP Pear :: Config Management Configuration Information
The Config class can manage your configuration files, whether it is stored in an XML file, INI file, php array, or other data resources: 1. Resolve different data formats; 2. Can be skilled Handling Sections, Directives, Comments, Blanks3 you are using. Re-save the configuration information into your favorite format (XML file, INI file, PHP array, or other data resources). The top object config_container: section The following can also include a config_container object; Directive: Does not contain other objects, but there is a pair of data similar to key-value form; Comment: blank: The data format that can be set is: XmlphParrayiniFileApache conf (.conf)
Suppose we have to save employees as the following form XML Version = "1.0" Encoding = "UTF-8"?>
$ c = new config (); $ root = & $ c-> Parseconfig ($ conf, 'phparray');
$ storage = & $ root-> getItem ('section', 'storage'); // Delete, then increase, change it to appear $ storage-> removeItem (); $ root-> additem ($ storage); // echo '
'. htmlspecialchars ($ root-> torstring ('XML', array ")))). ' pre>';IF ($ C-> Writeconfig ('D: /TMP/config.xml', 'XML', Array ('Name' => 'Company') === True) {Echo 'Config Written Into D: / TMP /config.xml ';}?> Read configuration file php require_once ("config.php"); $ c = new config (); $ root = & $ c-> parseconfig ("D: / tmp / member .xml, "XML"); // You can also read configuration information from other places such as: / ** $ conf = array ('db' => Array ('type' => 'mysql', 'Host' => 'LocalHost', 'user' => 'user', 'pass' => 'pass')); $ root = & $ c-> parseconfig ($ conf, 'phparray', array ('name' => 'conf'); * / if (Pear :: Iserror ($ root)) {Die ('Error While Reading Configuration:'. $ root-> getMessage ());} $ settings = $ root-> toArray () PRINTF ('user settings: % s% s/A>', $ settings ['root'] ['Company'] ['member'] ['www'], $ Settings ['root'] ['Company'] ['Name'] ['Name'], $ settings ['root'] ['Company'] ['MEMBER'] ['Email']);?> below Complex, practical: have the following configuration file XML Version = "1.0" encoding = "ISO-8859-1">
/ global-batteries> uk> Website>
SQL driver> mysql phptype> localhost hostspec> Mamasam username > foobar password> component> storage> company> First, let's save it in the XML file: //write.php PhpRequire_once ('config.php'); $ Conf ['Storage'] ['Driver'] = 'SQL'; $ conf ['storage'] ['params'] ['phpoType'] = 'mysql'; $ conf ['storage'] ['params'] ['HostSpec'] = 'LocalHost'; $ conf ['storage'] ['params'] ['username'] = 'mamasam'; $ conf ['storage'] ['params'] ['password'] = 'foobar'; $ conf ['menu'] ['laptop'] = array ('IBM' => 'IBM', 'Sony' => 'Sony'); $ Conf ['Website'] ['UK'] ['b2c-battery'] = 'http://www.b2c-battery.co.uk'; $ conf ['Website'] ['UK'] ['Global-Batteries'] =' http: // www .global-batteries.co.uk '; $ c = new config (); $ root = & $ c-> parseconfig ($ conf,' phparray '); $ storage = & $ root-> getItem ('section', 'storage'); // Delete, then increase, change it to appear $ storage-> removeItem (); $ root-> additem ($ storage); // echo '
'. htmlspecialchars ($ root-> torstring ('XML', array ")))). ' pre>';IF ($ C-> Writeconfig ('D: /TMP/config.xml', 'XML', Array ('Name' => 'Company') === True) {Echo 'Config Written Into D: / TMP /config.xml ';}?> Ok, next is to read this configuration file, suppose we only need the database configuration
// read.php php request_once (' config.php ') $ C = new config (); $ root = & $ c-> parseconfig ('d: /tmp/config.xml', 'XML'); if (Pear :: ISERROR ($ root)) {DIE ($ root-> getMessage ());} // Read the message of the specified selection // I can't read this, I don't know if config problem, but the file for a simple XML structure is ok, will be attached later. Example // $ store = & $ root-> getitem ('section', 'storage'); // Because the above code does not do, so I have to use a stupid approach, so that the volume of large data is not high. . / / Do you have a good solution? Tell me. $ content = $ root-> toarray (); unset ($ root); $ storage = $ content ['root'] ['storage']; unset ($ content); echo (' '); Print_R ($ storage); echo (' pre> '); // This allows you to get a PHP array, and other operations look at yourself. > An example of gettem () read, its data structure is as follows: XML Version = "1.0" Encoding = "UTF-8"?>localhost host> Joe user> Secret pass> db456 dB> mysql>