PHP讀取設定檔類(php,ini,yaml,xml)

來源:互聯網
上載者:User
<?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'];       }   }   ?>    php/**  * 針對PHP的配置,如有設定檔  *config.php  <?php  $db = array();   // Enter your database name here:  $db['name'] = 'test';   // Enter the hostname of your MySQL server:  $db['host'] = 'localhost';  ?>   //具體調用:  include ('settings.php'); //原始環境假設每個類為單獨的一個類名.php檔案  // Load settings (PHP)  $settings = new Settings_PHP;  $settings->load('config.php');  echo 'PHP: ' . $settings->get('db.host') . '';  *  */   讀取INI檔案,主要用到parser_ini_file函數,該函數返回一個數組,如第二個參數為true時則返回多維陣列/**  * ini例子:config.ini  *   [db]  name = test  host = localhost   //調用例子:  $settings = new Settings_INI;  $settings->load('config.ini');  echo 'INI: ' . $settings->get('db.host') . '';  */   讀取XML檔案,需要用到XML_PARSER,xmllib.php在http://hudeyong926.iteye.com/admin/blogs/836048/**  * XML例子:config.xml  <?xml version="1.0" encoding="UTF-8"?>  <settings>  <db>      <name>test</name>      <host>localhost</host>  </db>  </settings>   // Load settings (XML)  $settings = New Settings_XML;  $settings->load('config.xml');  echo 'XML: ' . $settings->get('db.host') . '';  *  */   讀取YAML格式檔案,使用YAML必須使用到SPYC這個庫,相關連結在http://spyc.sourceforge.net//**  YAML配置例子: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有點過時??2。xml比較好,3。yaml很好,但是畢竟沒有標準化。4。txt要自己組織格式,開放性不好。5。類序列化。比較好,但是不熟悉的人使用比較麻煩!6。php定義常量(你不用修改資料嗎?)所以:xml最好。


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。