PHP configuration file reading class (php, ini, yaml, xml)-PHP source code

Source: Internet
Author: User
Tags xml example
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.