phpBB

Development Wiki

Difference between revisions of "PhpBB3.1/RFC/Config Class"

From phpBB Development Wiki

(Created page with "== Description == Encapsulate the global $config array in an object that can be passed around easily. This also improves unit testing because you can inject a custom config inst...")
 
(Increment)
 
Line 27: Line 27:
 
phpbb_config::increment($key, $increment, $cache = true)
 
phpbb_config::increment($key, $increment, $cache = true)
  
Increments an integer configuration value.
+
Atomically increments an integer configuration value. Use this to make sure, that multiple requests incrementing in parallel don't overwrite each other's value. They will all correctly increment the value by one.
  
 
<php>$config->increment('mymod_registered_count', 1);</php>
 
<php>$config->increment('mymod_registered_count', 1);</php>

Latest revision as of 14:16, 20 March 2012

Description

Encapsulate the global $config array in an object that can be passed around easily. This also improves unit testing because you can inject a custom config instance into the code you are testing. Also refactor set_config to be an instance method.

Motivation

Because $config is a global variable, and set_config is a global function, it is very difficult to unit test anything that relies on them.

Usage

Get

Read a configuration value.

$version $config['mymod_version'];

Set

phpbb_config::set($key, $value, $cache = true)

Set a configuration option's value.

$config->set('mymod_version''1.0.0');

Increment

phpbb_config::increment($key, $increment, $cache = true)

Atomically increments an integer configuration value. Use this to make sure, that multiple requests incrementing in parallel don't overwrite each other's value. They will all correctly increment the value by one.

$config->increment('mymod_registered_count'1);

Implementation

The new config classes make use of the new cache drivers. A driver is injected into the config class. There are two implementations of config. One array-based one (for tests) and a db-based one which provides persistence.

This feature is almost fully backwards-compatible. phpbb_config implements ArrayAccess, allowing it to be accessed as if it were an array. set_config and set_config_count are initialized with a config instance which is cached in a static variable. Use of these functions is deprecated however, new code should use the instance methods of the config object.

It makes use of autoloading.