PHP讀取四種設定檔(php,ini,yaml,xml)的例子

來源:互聯網
上載者:User

PHP讀取四種設定檔(php,ini,yaml,xml)的例子,代碼如下,大家自己看吧,說不好哪天就能用得上哦。

  1. /**

  2. * 讀取4中配置的表資訊,現支援php.ini,xml.yaml
  3. */
  4. class Settings{
  5. var $_settings = array();
  6. /**
  7. * 擷取某些設定的值
  8. *
  9. * @param unknown_type $var
  10. * @return unknown
  11. */
  12. function get($var) {
  13. $var = explode('.', $var);

  14. $result = $this->_settings;

  15. foreach ($var as $key) {
  16. if (!isset($result[$key])) { return false; }

  17. $result = $result[$key];

  18. }

  19. return $result;

  20. // trigger_error ('Not yet implemented', E_USER_ERROR);//引發一個錯誤

  21. }

  22. function load() {

  23. trigger_error ('Not yet implemented', E_USER_ERROR);
  24. }
  25. }

  26. /**

  27. * 針對PHP的配置,如有設定檔
  28. * $file=
  29. $db = array();

  30. // Enter your database name here:

  31. $db['name'] = 'test';

  32. // Enter the hostname of your MySQL server:

  33. $db['host'] = 'localhost';

  34. ?>

  35. 具體調用:

  36. include ('settings.php'); //原始環境假設每個類為單獨的一個類名.php檔案

  37. // Load settings (PHP)

  38. $settings = new Settings_PHP;
  39. $settings->load('config.php');

  40. echo 'PHP: ' . $settings->get('db.host') . '';

  41. *

  42. */
  43. Class Settings_PHP Extends Settings {
  44. function load ($file) {
  45. if (file_exists($file) == false) { return false; }

  46. // Include file

  47. include ($file);
  48. unset($file); //銷毀指定變數
  49. $vars = get_defined_vars(); //返回所有已定義變數的列表,數組,變數包括伺服器等相關變數,
  50. //通過foreach吧$file引入的變數給添加到$_settings這個成員數組中去.
  51. foreach ($vars as $key => $val) {
  52. if ($key == 'this') continue;

  53. $this->_settings[$key] = $val;

  54. }
  55. }
  56. }

  57. //////////////////////讀取INI檔案,主要用到parser_ini_file函數,該函數返回一個數組,如第二個參數為true時則返回多維陣列/////////////////////

  58. /**
  59. * ini例子:
  60. * [db]
  61. name = test
  62. host = localhost
  63. 調用例子:
  64. $settings = new Settings_INI;
  65. $settings->load('config.ini');
  66. echo 'INI: ' . $settings->get('db.host') . '';

  67. *

  68. */
  69. Class Settings_INI Extends Settings {
  70. function load ($file) {
  71. if (file_exists($file) == false) { return false; }
  72. $this->_settings = parse_ini_file ($file, true);
  73. }
  74. }

  75. //////////////////////讀取XML檔案,需要用到XML_PARSER////////////////////////

  76. /**
  77. * XML例子:
  78. test
  79. localhost
  80. 調用例子:
  81. // Load settings (XML)
  82. $settings = New Settings_XML;
  83. $settings->load('config.xml');
  84. echo 'XML: ' . $settings->get('db.host') . '';
  85. *
  86. */
  87. Class Settings_XML Extends Settings {
  88. function load ($file) {
  89. if (file_exists($file) == false) { return false; }

  90. /**xmllib.php為PHP XML Library, version 1.2b,相關串連:http://keithdevens.com/software/phpxml

  91. xmllib.php主要特點是把一個數群組轉換成一個xml或吧xml轉換成一個數組
  92. XML_unserialize:把一個xml給轉換 成一個數組
  93. XML_serialize:把一個數群組轉換成一個xml
  94. 自PHP5起,simpleXML就很不錯,但還是不支援將xml轉換成數組的功能,所以xmlLIB還是很不錯的.
  95. */
  96. include ('xmllib.php');
  97. $xml = file_get_contents($file);
  98. $data = XML_unserialize($xml);
  99. $this->_settings = $data['settings'];
  100. }
  101. }
  102. //////////////////////////////////讀取YAML格式檔案///////////////////////////////////////////////
  103. /**
  104. 使用YAML必須使用到SPYC這個庫,相關連結在http://spyc.sourceforge.net/
  105. YAML配置例子:
  106. db:
  107. name: test
  108. host: localhost
  109. */
  110. Class Settings_YAML Extends Settings {
  111. function load ($file) {
  112. if (file_exists($file) == false) { return false; }

  113. include ('spyc.php');

  114. $this->_settings = Spyc::YAMLLoad($file);
  115. }
  116. }

複製代碼
  • 聯繫我們

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

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

    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.