PHP reads configuration file instances (including ini, yaml, and xml)

Source: Internet
Author: User
Tags xml example
This article mainly introduces the PHP configuration file reading class, which can read configuration files such as ini, yaml, and xml. it has some reference value, for more information about how to read configuration files from PHP, see the following example. Share it with you for your reference. The details are as follows:

<?php class Settings {  var $_settings = array ();  function get($var) {  $var = explode ( '.', $var );  $result = $this->_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 settings array  foreach ( $vars as $key => $val ) {   if ($key == 'this')   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'];  } } ?> 

/*** For PHP configuration, such as the configuration file * config. php <? Php $ db = array (); // Enter your database name here: $ db ['name'] = 'test'; // Enter the hostname of your MySQL server: $ db ['host'] = 'localhost';?> // 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 :'. $ s Ettings-> get ('Db. host'). ''; */to read the XML file, use XML_PARSER, xmllib. php/*** XML Example: config. xml <? Xml version = "1.0" encoding = "UTF-8"?>
  
    
   
    
Test
     
   
    
Localhost
    
   
 // Load settings (XML) $ settings = New Settings_XML; $ settings-> load ('config. xml '); echo 'XML :'. $ settings-> get ('Db. host '). ''; **/read the YAML format file. to use YAML, you must use the SPYC Library/** 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. is ini outdated ??
2. xml is better,
3. yaml is good, but it is not standardized after all.
4. txt needs to be organized in its own format, which is not easy to open.
5. class serialization. It is good, but it is troublesome for unfamiliar people to use it!
6. Define constants in php (do you not need to modify the data ?)

Therefore, xml is the best.

I hope this article will help you with php programming.

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.