PHP: Read configuration files such as inixml

Source: Internet
Author: User
Tags xml example
We often use data storage when developing programs. Generally, if there is not much data, we usually choose to save the data on the local configuration. The most common formats of configuration files include inixml and other formats. I don't know why we save data in text files. Maybe it's reading speed.

We often use data storage when developing programs. Generally, if there is not much data, we usually choose to save the data on the local configuration. The most common formats of configuration files include ini xml and other formats. I don't know why we save data in text files. Maybe it's reading speed.

We often use data storage when developing programs. Generally, if there is not much data, we usually choose to save the data on the local configuration. The most common formats of configuration files include ini xml and other formats. I don't know why we save data in text files. Maybe the difference in reading speed. Now let's take a look at how PHP reads data from the local configuration file and obtains what it needs.

 _ 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 '). "; */to read XML files, 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.

Of course, if the data is huge, we need to use the database to store the data. For execution speed and security ......

Reprinted, please note: IT passers-by» PHP: Read ini xml and other configuration files

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.