Config Class load config.txt


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Config {
  2.  
  3. private static $instance = null;
  4. private $file_path = dirname(__FILE__);
  5. private $file_name = 'config';
  6. private $file_extention = 'txt';
  7. private $split_letter = ':';
  8.  
  9. private $config;
  10.  
  11. private function __construct() {
  12. $this->config = $this->parse($this->file_path . '/' . $this->file_name . '.' . $this->file_extention);
  13. }
  14.  
  15. private function prase($file) {
  16. if(file_exist($file)) {
  17. $data = file($file);
  18. }
  19. foreach($data as $key => $value) {
  20. $split = explode($this->split_letter, $value);
  21. $config[$split[0]] = $split[1];
  22. }
  23. return $config;
  24. }
  25.  
  26. public static function getInstance() {
  27. if (self::$instance === NULL) {
  28. self::$instance = new Config();
  29. }
  30. return self::$instance;
  31. }
  32.  
  33. public function __get($var) {
  34. return $this->config[$var];
  35. }
  36.  
  37. private function __clone() {}
  38. }
  39.  
  40. // config.txt could be
  41. // format: key:value
  42. language:en
  43. controller:1

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.