Skip to content

Instantly share code, notes, and snippets.

@millsoft
Last active August 20, 2017 10:52
Show Gist options
  • Save millsoft/4c3f182509e6cbc4dfdfb8ad297c856f to your computer and use it in GitHub Desktop.
Save millsoft/4c3f182509e6cbc4dfdfb8ad297c856f to your computer and use it in GitHub Desktop.
A simple Configuration Class for loading and saving to local config.json file.
<?php
/**
* Configuration
*/
class Conf{
public static $configFile = "config.json";
private static $currentConfig = array();
public static function get($key, $default = null){
if(!isset(self::$currentConfig[$key])){
return $default;
}
return self::$currentConfig[$key];
}
public static function set($key, $value){
self::$currentConfig[$key] = $value;
self::save();
}
/**
* Save current config from memory to config file
*/
private static function save(){
$json = json_encode(static::$currentConfig);
file_put_contents(static::$configFile, $json);
}
/**
* Load config file:
*/
private static function load(){
if(!file_exists(static::$configFile)){
//create a new config file:
$emptyConf = json_encode(array());
file_put_contents(static::$configFile, $emptyConf);
}
//read config file to array:
$C = json_decode(file_get_contents(static::$configFile), true);
static::$currentConfig = $C;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment