Best practice for storing settings for a website?

Initially, I intend to create a class (Object oriented) to save the settings as permanent. For example, in PHP:

class Settings{
     const test = 'foobar!';
}

But later I think that this approach does not allow the admin user to change these parameters. It seems that only the parameters optional for the change should be declared as constant, while others should be declared as private variables, then the application will detect whether the parameters will be available in the database. First set the parameter table, and if not, it will be used by default from the get (), set () function defined in the settings class. Is this a good approach? What is the best approach? I appreciate any advice on this issue.

+3
source share
4 answers

, = > . , db.

$value = Classname::getparam('configsetting');
Classname::setparam('configsetting', $newvalue);

make/set param mehods public static!

...

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;
     }
 } 
+3

:: , .

, , mysql , , , .

+1

, YAML.

0

I usually save parameter values ​​that are not needed to change as constants and other parameters that have the change parameter in the database (getter / setter). Then I cache the data for a longer period of time, so I do not need to access the database for each request. When the administrator changes any settings, the cache is invalid and the new value is stored in the cache.

There is another way to store configuration data in XML files. Can someone tell me what is the advantage of storing settings data in an XML file compared to storing in a database?

0
source

All Articles