INI.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. class Core_Config_INI {
  3. var $_config_file;
  4. var $_data;
  5. function __construct( $config_file ) {
  6. $this->_config_file = $config_file;
  7. $this->_data = null;
  8. }
  9. /**
  10. * @return an associative array on success, and FALSE on failure.
  11. */
  12. function &getData() {
  13. if ($this->_data === null) {// @see __construct()
  14. if (!file_exists($this->_config_file)) {
  15. $this->_data = false;
  16. trigger_error('config file not exists', E_USER_WARNING);
  17. } else {
  18. $this->_data = parse_ini_file( $this->_config_file, true );// @return false if error
  19. }
  20. }
  21. return $this->_data;
  22. }
  23. function get( $key, $section = null ) {
  24. $_data = $this->getData();
  25. if ($_data == false) {
  26. return false;
  27. }
  28. if (null != $section) {
  29. if (array_key_exists($section, $_data)) {
  30. if (array_key_exists($key, $_data[ $section ])) {
  31. return $_data[ $section ][ $key ];
  32. } else {
  33. trigger_error('config error: key('.$key.') not exists in section ('.$section.')', E_USER_NOTICE);
  34. }
  35. } else {
  36. trigger_error('config error: section not exists: ('.$section.')', E_USER_NOTICE);
  37. }
  38. }
  39. else {
  40. if (array_key_exists($key, $_data)) {
  41. return $_data[ $key ];
  42. } else {
  43. trigger_error('config error: key not exists ('.$key.')', E_USER_NOTICE);
  44. }
  45. }
  46. return false;
  47. }
  48. }//class