PHP reads the php code of the configuration file class (php, ini, yaml, xml:
_ Settings; foreach ($ var as $ key) {if (! Isset ($ result [$ key]) {return false;} $ result = $ result [$ key];} return $ result;} function load () {trigger_error ('not yet implemented', E_USER_ERROR);} class Settings_PHP extends Settings {function load ($ file) {if (file_exists ($ file) = false) {return false;} // Include file include ($ file); unset ($ file); // Get declared variables $ vars = get_defined_vars (); // Add to set Jsonarray foreach ($ vars as $ key => $ val) {if ($ key = 'eas') continue; $ this-> _ settings [$ key] = $ val ;}} class Settings_INI extends Settings {function load ($ file) {if (file_exists ($ file) = false) {return false;} $ this-> _ settings = parse_ini_file ($ file, true) ;}} class Settings_YAML extends Settings {function load ($ file) {if (file_exists ($ file) = false) {return false;} Include ('spyc. php '); $ this-> _ settings = Spyc: YAMLLoad ($ file) ;}} class Settings_XML extends Settings {function load ($ file) {if (file_exists ($ file) = false) {return false;} include ('xmllib. php '); $ xml = file_get_contents ($ file); $ data = XML_unserialize ($ xml ); $ this-> _ settings = $ data ['Settings'] ;}}?> Php/*** for PHP configuration, such as the configuration file * config. php
// Specific call: include ('settings. php '); // The original environment assumes that each class is a separate class name. PHP file // Load settings (PHP) $ settings = new Settings_PHP; $ settings-> load ('config. php '); echo 'php :'. $ settings-> get ('Db. host '). ''; **/read the ini file, mainly using the parser_ini_file function. This function returns an array. if the second parameter is true, a multi-dimensional array is returned. *** ini example: config. ini * [db] name = test host = localhost // call example: $ settings = new Settings_INI; $ settings-> load ('config. ini '); echo 'ini :'. $ settings-> get ('Db. host '). ''; */read the XML file. XML_PARSER and xmllib are required. php in http://hudeyong926.iteye.com/admin/blogs/836048/*** XML Example: config. xml
Test
Localhost
// Load settings (XML) $ settings = New Settings_XML; $ settings-> load ('config. xml '); echo 'XML :'. $ settings-> get ('Db. host '). ''; **/read the YAML format file, using YAML must use the SPYC Library, related links in the http://spyc.sourceforge.net // ** YAML configuration example: config. yaml db: name: test host: localhost // Load settings (YAML) $ settings = New Settings_YAML; $ settings-> load ('config. yaml '); echo 'yaml :'. $ settings-> get ('Db. host '). ''; */1. Ini is a little outdated ?? 2. Xml is better, 3. Yaml is good, but it is not standardized after all. 4. The txt file must be in its own format, which is not open. 5. Class serialization. It is good, but it is troublesome for unfamiliar people to use it! 6. Php defines constants (do you not need to modify data ?) Therefore, xml is the best.