PHP读取配置文件类实例(可读取ini,yaml,xml等)

这篇文章主要介绍了PHP读取配置文件类,可读取ini,yaml,xml等配置文件,具有一定参考借鉴价值,需要的朋友可以参考下,本文实例讲述了PHP读取配置文件类实例,分享给大家供大家参考,具体如下:

  1. <?php
  2. class Settings {
  3. var $_settings = array ();
  4. function get($var) {
  5. $var = explode ( '.', $var );
  6. $result = $this->_settings;
  7. foreach ( $var as $key ) {
  8. if (! isset ( $result [$key] )) {
  9. return false;
  10. }
  11. $result = $result [$key];
  12. }
  13. return $result;
  14. }
  15. function load() {
  16. trigger_error ( 'Not yet implemented', E_USER_ERROR );
  17. }
  18. }
  19. class Settings_PHP extends Settings {
  20. function load($file) {
  21. if (file_exists ( $file ) == false) {
  22. return false;
  23. }
  24. // Include file
  25. include ($file);
  26. unset ( $file );
  27. // Get declared variables
  28. $vars = get_defined_vars ();
  29. // Add to settings array
  30. foreach ( $vars as $key => $val ) {
  31. if ($key == 'this')
  32. continue;
  33. $this->_settings [$key] = $val;
  34. }
  35. }
  36. }
  37. class Settings_INI extends Settings {
  38. function load($file) {
  39. if (file_exists ( $file ) == false) {
  40. return false;
  41. }
  42. $this->_settings = parse_ini_file ( $file, true );
  43. }
  44. }
  45. class Settings_YAML extends Settings {
  46. function load($file) {
  47. if (file_exists ( $file ) == false) {
  48. return false;
  49. }
  50. include ('spyc.php');
  51. $this->_settings = Spyc::YAMLLoad ( $file );
  52. }
  53. }
  54. class Settings_XML extends Settings {
  55. function load($file) {
  56. if (file_exists ( $file ) == false) {
  57. return false;
  58. }
  59. include ('xmllib.php');
  60. $xml = file_get_contents ( $file );
  61. $data = XML_unserialize ( $xml );
  62. $this->_settings = $data ['settings'];
  63. }
  64. }
  65. ?>
  66. /**
  67. * 针对PHP的配置,如有配置文件
  68. *config.php
  69. <?php
  70. $db = array();
  71. // Enter your database name here:
  72. $db['name'] = 'test';
  73. // Enter the hostname of your MySQL server:
  74. $db['host'] = 'localhost';
  75. ?>
  76. //具体调用:
  77. include ('settings.php'); //原始环境假设每个类为单独的一个类名.php文件
  78. // Load settings (PHP)
  79. $settings = new Settings_PHP;
  80. $settings->load('config.php');
  81. echo 'PHP: ' . $settings->get('db.host') . '';
  82. *
  83. */
  84. 读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组
  85. /**
  86. * ini例子:config.ini
  87. *
  88. [db]
  89. name = test
  90. host = localhost
  91. //调用例子:
  92. $settings = new Settings_INI;
  93. $settings->load('config.ini');
  94. echo 'INI: ' . $settings->get('db.host') . '';
  95. */
  96. 读取XML文件,需要用到XML_PARSER,xmllib.php
  97. /**
  98. * XML例子:config.xml
  99. <?xml version="1.0" encoding="UTF-8"?>
  100. <settings>
  101. <db>
  102. <name>test</name>
  103. <host>localhost</host>
  104. </db>
  105. </settings>
  106. // Load settings (XML)
  107. $settings = New Settings_XML;
  108. $settings->load('config.xml');
  109. echo 'XML: ' . $settings->get('db.host') . '';
  110. *
  111. */
  112. 读取YAML格式文件,使用YAML必须使用到SPYC这个库<a href="http://spyc.sourceforge.net//">
  113. /</a>**
  114. YAML配置例子:config.yaml
  115. db:
  116. name: test
  117. host: localhost
  118. // Load settings (YAML)
  119. $settings = New Settings_YAML;
  120. $settings->load('config.yaml');
  121. echo 'YAML: ' . $settings->get('db.host') . '';
  122. */

1. ini有点过时??

2. xml比较好,

3. yaml很好,但是毕竟没有标准化。

4. txt要自己组织格式,开放性不好。

5. 类序列化。比较好,但是不熟悉的人使用比较麻烦!

6. php定义常量(你不用修改数据吗?)

所以:xml最好。

希望本文所述对大家的php程序设计有所帮助。