Hmm looking at the JSON formatted like that leads me to this thought: file_get_contents on the globbed files and concatenated together like this:
// db.json
"database": {
"host": "localhost",
"login": "foo",
"password": "bar"
}
// mail.json
"mail": {
"smtp_host": "mail.foo.bar",
"user": "john",
"password": "doe"
}
// ConfigHandler.class.php
class ConfigHandler {
public $config = array();
private static $_instance = NULL;
public static function getInstance() {
if( is_null(self::$_instance) ) {
self::$_instance = new ConfigHandler();
}
return self::$_instance;
}
private function __construct() {
$files = glob(APPROOT .'config/*.json');
$cfg = '{';
foreach( $files as $file ) {
$cfg .= file_get_contents($file) .',';
}
$cfg = rtrim($cfg,',') .'}';
$cfg = json_decode($cfg);
foreach( $cfg as $k => $v ) {
if( is_array($v) ) {
$this->config[$k] = new Config($v);
} else {
$this->config[$k] = $v;
}
} //foreach
} //construct
}//class
Questions: Did I do singleton right? Do you think I should do sub-classes still? In which case I might have to dynamically call that class, something like:
$class = $k.'Config';
$this->config[$k] = new $class($v);
I feel like having identical subclasses is a waste, I would end up just doing the following since they wouldn't have to behave differently:
abstract class Config {
public $settings = array();
public function __construct() {
// construct code
}
public function __get($k) {
return $this->settings[$k];
}
public function __set($k,$v) {
$this->settings[$k] = $v;
}
}
class DatabaseConfig extends Config {}
class MailConfig extends Config {}
Maybe I just don't understand, but what is the point then of even creating the subclasses if they don't have different methods and/or properties.