Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Config{
- private $_config = NULL;
- private $_filepath = NULL;
- public function __construct($filepath){
- $this->_filepath = $filepath;
- $this->load();
- }
- private function load(){
- if (is_array($this->_config))
- return;
- if (!file_exists($this->_filepath))
- throw new Exception('Configuration file not found');
- $this->_config = parse_ini_file($this->_filepath);
- }
- public function get($key){
- if ($this->_config === NULL)
- throw new Exception('Configuration file is not loaded');
- if (isset($this->_config[$key]))
- return $this->_config[$key];
- else
- throw new Exception('Variable ' . $key . ' does not exist in configuration file');
- }
- }
- #config instantiation
- $config = new Config(ABSPATH . '/app/config.ini');
- #Any function which uses config
- function dbconnect($DBhost, $DBuser, $DBpass, $DBname){
- $db = mysqli_connect($DBhost, $DBuser, $DBpass, $DBname);
- if (!$db)
- throw new Exception('MySQL connection failed');
- else
- return $db;
- }
- try{
- dbconnect($config->get('DBhost'), $config->get('DBuser'), $config->get('DBpass'), $config->get('DBname'));
- }catch(Exception $e){
- echo $e->getMessage();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement