欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-06-13 23:13:28
...
  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. ?>
复制代码