Best practice for storing settings for a website?
php, settings
Solution
I a prefer similar to your method, but using a private static property key=>value array with a getter and setter. That way later you can tie them to a file or db.
$value = Classname::getparam('configsetting');
Classname::setparam('configsetting', $newvalue);
Dont forget to make the get/set param mehods public static!
Here's the full class would look something like...
class Classname{
private static $params = null;
public static function getparam($key){
if(is_null(self::$params){
self::$params = array();
//initialize param array here from file, db, or just hardcoded values...
}
return isset(self::$params[$key])?self::$params[$key]:null;
}
public static function setparam($key, $value){
if(is_null(self::$params){
self::$params = array();
//initialize array here
}
self::$params[$key] = $value;
}
}
Problem
Originally, my intention is to create a class (Object oriented) to store settings as constant. For example, in PHP: ``` class Settings{ const test = 'foobar!'; } ``` But later on, I think that this approach does not let the Admin user modify these settings. It seems that only non-necessary-to-modify settings should be declared as constant, and others should be declared as private variable, then the application will find whether settings are available in the database Setting table first, if not, it will use default from get(), set() function defined in Settings class. Is that a good approach? What is the best approach? I appreciate any pieces advice on this issue.