Hi guys,
In an effort to continue reducing my reliance on global variables I thought I would make a simple config registry. Here is the code for the class:
<?php
namespace core;
class config {
use traits\backtrace; // vBulletin seems to be removing the namespace slashes
public $settings = array();
public function __construct(Array $settings) {
foreach( $settings as $k => $v ) {
if( is_array($v) ) {
$this->settings[$k] = new config($v);
} else {
$this->settings[$k] = $v;
}
}
}
public function __get($name) {
if( isset($this->settings[$name]) ) return $this->settings[$name];
trigger_error('Undefined property '.$name.$this->getCallerTrace(),E_USER_ERROR);
return NULL;
}
public function __set($name,$val) {
$this->settings[$name] = $val;
}
public function __isset($name) {
return ( isset($this->$name) || isset($this->settings[$name]) );
}
public function __unset($name) {
if( isset($this->settings[$name]) ) unset($this->settings[$name]);
}
}
And of course his is an extremely simplified use case:
$config = array();
$config['database']['engine'] = 'mysql';
$config['database']['user'] = 'username';
$config['database']['pass'] = 'password';
$config['database']['schema'] = 'database';
$config['database']['host'] = 'localhost';
$config['mail']['type'] = 'smtp';
$config['mail']['host'] = 'localhost';
$config['mail']['post'] = 587;
$config['mail']['user'] = 'user@domain.net';
$config['mail']['pass'] = 'mysupersecretemailpassword';
$config = new \core\config($config); // For some reason vBulletin is removing slashes here that point to the namespace
$PDO = new PDO($config->database->engine .':dbname='. $config->database->schema .';host='. $config->database->host, $config->database->user, $config->database->pass);
$mail = new Mailer($config->mail->host, $config->mail->port, $config->mail->user, $config->mail->pass, $config->mail->type);
Basically I'm not even sure if this is the right way to do a config registry (or if it even qualifies as a registry) but I was hoping you guys would give me your feedback (or critique as it were) on this simple class.