Examples of PHP reading four configuration files (php, ini, yaml, xml)

WBOY
Release: 2016-07-25 09:11:47
Original
1004 people have browsed it

Examples of PHP reading four configuration files (php, ini, yaml, xml). The code is as follows. Take a look at it for yourself. You may be able to use it one day.

  1. /**

  2. * Read the table information configured in 4, now supports php.ini, xml.yaml
  3. */
  4. class Settings{
  5. var $_settings = array();
  6. /**
  7. * Get the value of some settings
  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. * For PHP configuration, if there is a configuration file
  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. < ;p>?>

  35. Specific call:

  36. include ('settings.php'); //The original environment assumes that each class is a separate class name .php file
  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 example:
  60. * [db]
  61. name = test
  62. host = localhost
  63. Call example:
  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 is PHP XML Library, version 1.2b, related link: http://keithdevens.com/software/phpxml

  91. The main feature of xmllib.php is to convert an array into an xml or convert xml into an array
  92. XML_unserialize: Convert an xml into an array
  93. XML_serialize: Convert an array into an xml
  94. Since PHP5, simpleXML is very good, but it still does not support the function of converting xml into an array, so xmlLIB is still very good.
  95. */
  96. include ('xmllib.php');
  97. $xml = file_get_contents($file);
  98. $data = XML_unserialize($xml);
  99. $this->_settings = $data['settings'];
  100. }
  101. }
  102. ////////////////////////////////////Read YAML format files////////// //////////////////////////////////////
  103. /**
  104. To use YAML, you must use the SPYC library. The relevant link is at http://spyc.sourceforge.net/
  105. YAML configuration example:
  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. }

Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!