PHP Read configuration file class (Php,ini,yaml,xml)

Source: Internet
Author: User
Tags foreach format array include ini php class php file vars
<?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) = = f   
        Alse) {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_exis   
        TS ($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) = = f   
        Alse) {return false;   
        Include (' xmllib.php ');   
        $xml = file_get_contents ($file);   
           
      $data = Xml_unserialize ($xml);  $this->_settings = $data [' Settings '];  
 
}?> PHP/** * For PHP configuration, such as 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 used to the Parser_ini_file function, the function returns an array, such as the second argument is true when the multidimensional array/** * INI Example: config.ini * [db] name = Test  
host = localhost//invocation example: $settings = new Settings_ini;  
$settings->load (' Config.ini '); Echo ' INI: '. $settings->get (' Db.host ').  
''; * * Read XML file, need to use xml_parser,xmllib.php in http://hudeyong926.iteye.com/admin/blogs/836048/** * XML Example: Config.xml <?x ML version= "1.0" encoding= "UTF-8"?> <settings> <db> <name>test</name>  


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.