I am after some input/guidance.. I have an application that uses a central configuration registry as show below..
The application parses a config directory for ini files and sets the arrays into the class using the name of the file as the index, as well as single configuration variables when required..
I can see a few problems arising including:
- Name clashes between files and vars set within the script
-
The inability to pull nested array variables resulting in the following code:
$databases = config::get(‘database’);
$actual_record = $databases[‘default’];
I was tempted to add a 2nd get parameter for the nested value, however what happens in the future if i need to pull a 3rd or 4th level value.
class config
{
private static $registry;
/**
*
*/
private function __construct() {}
/**
*
*/
public static function get($key)
{
if (isset(self::$registry[$key])) return self::$registry[$key];
else return FALSE;
}
/**
*
*/
public static function set($key, $value, $overwrite = FALSE)
{
// Does the variable already exist?
if (isset(self::$registry[$key]) && $overwrite === FALSE)
throw new Exception();
self::$registry[$key] = $value;
}
}
Thanks in advance for the help..
As proposed in the comments here is the code to make the dot separated names working. There might be a more efficient solution, I just threw this together for you.