Author: Thomas Björk
Updated on: 2014-08-01
Posted on: 2014-08-01
Package: Fuse Array
By using Fuse Array it gets much easier to handle configurations in a project.
One of the more compilcated problems to solve in a project is how to handle the configuration. Either you can define a whole bunch of constants or you can create an array with the settings you wish to use.
The problem with constants is that they can't be changed. On the other hand, an array with values can be changed.
But sometimes you might want some parts of the configuration to be read-only and some parts changable.
By using Fuse Array this becomes possible.
Let's look at a simple example.
Configuration file (config.ini)
[database] host=localhost type=pdo username=user password=pass [paths] public=/var/www/public system=/var/www/system
include('./extends/configarray.php'); $GLOBALS['config'] = ConfigArray::ConvertIni('/config.ini'); $GLOBALS['config']->Set('paths.cache', '/var/www/cache'); ... ...
Later on in the code you may use $GLOBALS['config']->Get('desired key') to get a value from the configuration.
The values from the file config.ini is read-only, while the key "paths.cache" may be changed.
This solves some of the problems when you need to handle values defined in an external configuration file.
I hope that you can find this as useful as I did.
You need to be a registered user or login to post a comment
1,532,623 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
No comments were submitted yet.