Skip to content

Instantly share code, notes, and snippets.

@TemporaryJam
Last active August 29, 2015 13:56
Show Gist options
  • Save TemporaryJam/9234612 to your computer and use it in GitHub Desktop.
Save TemporaryJam/9234612 to your computer and use it in GitHub Desktop.
PHP config class supporting dot notation
class Config
{
/**
*@var array
*/
private static $config = null;
/**
* Returns a config setting.
* @param string $name
* @return mixed
*/
public static function get($name)
{
if (is_null(self::$config)) {
$base_config = require_once('config_base.php');
$site_config = include_once('config.php');
$config = array_merge($base_config, $site_config);
self::$config = $config;
}
if (strpos($name, ".")) {
$parts = explode(".", $name);
$val = self::$config;
foreach ($parts as $part){
if (isset($val[$part])) {
$val = $val[$part];
} else {
throw new \Exception("Unknown setting '" . $name . "' requested.");
}
}
return $val;
} else {
if (isset(self::$config[$name])) {
return self::$config[$name];
} else {
throw new \Exception("Unknown setting '" . $name . "' requested.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment