Ini File Configuration Parser Class

Submitted by:Jhon Jhon

Date added:16 February, 2013

Category:PHP

How to serve configuration settings from a config.ini file

Tags: parser class

Code Snippet:

<?php

class IniConfig {
private $_section = '';
private $_config = array();

public function __construct($file, $section = ''){
$bool = ($section) ? true : false;
$this->_config = parse_ini_file($file, $bool);
$this->_section = $section;
}

public function __get($key){
if ( isset($this->_config[$this->_section]) ){
if ( isset($this->_config[$this->_section][$key]) ){
return $this->_config[$this->_section][$key];
}
}else{
if ( isset($this->_config[$key]) ){
return $this->_config[$key];
}
}
}
}

// Example usage

$config = new IniConfig('config.ini', 'live');
echo $config->username . ' ' . $config->password
?>

// config.ini
[live]
username = 'live_user'
password = 'password1234'

[test]
username = 'test_user'
password = 'password4321'
 
 

Comments