Home >Backend Development >PHP Tutorial >PHP class for reading configuration files PHP reads ini, yaml, xml configuration file information

PHP class for reading configuration files PHP reads ini, yaml, xml configuration file information

WBOY
WBOYOriginal
2016-07-25 08:56:08950browse
  1. /**
  2. * Function: Read configuration file
  3. * Editor: bbs.it-home.org
  4. * Last modified: 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. ?>
复制代码


Statement:
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