Model of settings in a configuration table
Currently, I store some settings in a database table. It contains 2 fields (setting_name, setting_value).
Now, all my models need access t开发者_JAVA百科o these settings (for example, there is a setting which decides how many records a query should return at max), but I'm not sure how to implement it with good OO practices. Is there an accepted/good way of doing this (so all models have access to the config settings)?
Also, should I cache these settings (retrieving them is actually just 1 query per pageload) as some people seem to recommend this?
Thanks
I handle this through my Dependency Injection Container a.k.a. Service Container. When the container is initialised, I simply execure the query and create an associative array (well, ArrayObject really) out of all the config settings. Then I register this array as a service in my Service Container. That way, my entire application can easily access the configuration like so:
$config = $this->container->get('configuration');
do_something($config['bar']);
The solution to this problem is the Singleton pattern.
You should have a single instance class with static methods to get configuration values:
Config::getConfigData($conf_key)
Caching: it depends on how many configuration data you have in your db. I suggest to cache those and not to perform a query every time you need a value.
精彩评论