PHP read config file class php read ini, YAML, XML configuration file information

Source: Internet
Author: User
Tags vars
  1. /**
  2. * Function: Read the 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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.