PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

PHP读取配置文件的类 php读取ini、yaml、xml配置文件信息

原创
2016-07-25 08:56:08 695浏览
  1. /**
  2. * 功能:读取配置文件
  3. * 编辑:bbs.it-home.org
  4. * 最后修改:2013/10/11
  5. */
  6. class Settings {
  7. var $_settings = array();
  8. function get($var) {
  9. $var = explode('.', $var);
  10. $result = $this->_settings;
  11. foreach ($var as $key) {
  12. if (!isset ($result [$key])) {
  13. return false;
  14. }
  15. $result = $result [$key];
  16. }
  17. return $result;
  18. }
  19. function load() {
  20. trigger_error('Not yet implemented', E_USER_ERROR);
  21. }
  22. }
  23. class Settings_PHP extends Settings {
  24. function load($file) {
  25. if (file_exists($file) == false) {
  26. return false;
  27. }
  28. // Include file
  29. include ($file);
  30. unset ($file);
  31. // Get declared variables
  32. $vars = get_defined_vars();
  33. // Add to settings array
  34. foreach ($vars as $key => $val) {
  35. if ($key == 'this')
  36. continue;
  37. $this->_settings [$key] = $val;
  38. }
  39. }
  40. }
  41. class Settings_INI extends Settings {
  42. function load($file) {
  43. if (file_exists($file) == false) {
  44. return false;
  45. }
  46. $this->_settings = parse_ini_file($file, true);
  47. }
  48. }
  49. class Settings_YAML extends Settings {
  50. function load($file) {
  51. if (file_exists($file) == false) {
  52. return false;
  53. }
  54. include ('spyc.php');
  55. $this->_settings = Spyc::YAMLLoad($file);
  56. }
  57. }
  58. class Settings_XML extends Settings {
  59. function load($file) {
  60. if (file_exists($file) == false) {
  61. return false;
  62. }
  63. include ('xmllib.php');
  64. $xml = file_get_contents($file);
  65. $data = XML_unserialize($xml);
  66. $this->_settings = $data ['settings'];
  67. }
  68. }
  69. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。