NewsFeaturesDownloadsDevelopmentSupportAbout Us
Global Configuration API

Global Configuration API

From LifeType Wiki

Contents

[edit] The global configuration API

The Config class takes care of dealing with configuration. Configuration in LifeType is normally saved in the database but data can also be saved to a .properties.php file if needed.

The Config class provides methods for saving, retrieving and dealing with configuration settings (also known as "keys") The Config class takes care of transforming the data so that it can be correctly saved to the storage, and this is specially important when saving and retrieving more complex structures such as arrays and objects. The class will detect the type of the data to be saved (or loaded) and will act accordingly so that the data is the same after being saved and once it's been loaded.

[edit] How to get a reference to the configuration settings

The static method Config::getConfig() is the preferred way. It will first see if there already is a handler open and return a reference to that object. Or in other words, if we have already called Config::getConfig() from somewhere else it will return a reference to the same object so that we don't have to create a new one. This will save memory and it is better for performance, as every time a new Config object is created, we are loading all the setings from the database.


 $config =& Config::getConfig();

Please remember to use the "=&" operator, otherwise $config would be just a copy instead of a reference to the original object!

If needed, we can still create new objects via the traditional way:

 $config = new Config();


[edit] How to retrieve a value

Use the Config::getValue($key) method:

 $config =& Config::getConfig();
 $value = $config->getValue( "value_key" );

It will return the value associated to the given key, or the empty string if there is no value associated with this key.

[edit] How to save a value

Use the Config::setValue($key,$value) method followed by a Config::save() method, which will synchronize the information with the database tables. Old keys will be updated while any new key will be added. Old keys will *not* be removed.

 $config =& Config::getConfig();
 $config->setValue( "value_key", $value );
 $config->save();

Alternatively, we can use Config::saveValue($key,$value) if we are only going to save one value (less overhead):

 $config =& Config::getConfig();
 $result = $config->saveValue( "value_key", $value );

Returns true if successful or false otherwise.

[edit] Other methods

  • Config::reload(): Reloads the contents from the configuration backend.
  • Config::getKeys(): Returns an array of strings containing the name of all the keys.
  • Config::getValues(): Returns an array containing all the values found in the congfiguration.
  • Config::getAsArray(): Returns an associative array of pairs (key, value) containing all the data found in the configuration backend.