I’ve decided to list all known methods along with their advantages and disadvantages.
I’ve marked this answer as a community wiki so collaboration is easier.
Global Constants
Assigning:
define('CONFIG_DIRECTIVE', 'value');
Accessing:
$object = new MyObject(CONFIG_DIRECTIVE);
Advantages:
- Has global scope.
- Autocompleted by most IDEs.
- Has an agreed upon naming convention (UPPERCASE_UNDERSCORE_SEPARATED).
Disadvantages:
- Directives cannot contain arrays (prior to v7.0.0).
Special Notes:
- Cannot be reassigned.
Alternate Syntax Files
For example: XML, INI, YAML, etc.
Assigning:
- Simply edit the file in it’s specific language. (For example, for INI files:
config_directive = value.)
Accessing:
- The config file needs to be parsed. (For example, for INI’s:
parse_ini_file().)
Advantages:
- Most likely has a syntax more suited for a config file.
Disadvantages:
- Possible overhead of accessing and parsing the file.
Array
Assigning:
$config['directive'] = 'value';
Accessing:
- The cleanest method of accessing configuration values using this method is to pass the required values to the object that needs them on creation, or pass them to your container object and let it handle passing them out internally.
$object = new MyObject($config['directive']);$container = new MyContainer($config);
Advantages:
- Directives can be arrays.
Disadvantages:
- No autocompletion.
Special Notes:
- Variable collisions can occur. If this is a concern, name your array appropriately to avoid them.
Class
Assigning:
- There are many different class based implementations.
- Static class.
myCfgObj::setDirective('DIRECTIVE', 'value');
- Instanced class.
myCfgObj->setDirective('DIRECTIVE', 'value');
- Static class.
Accessing:
- Again there are various class based implementations.
- Static class.
$object = new MyObject(myCfgObj::getDirective('DIRECTIVE'));
- Instanced class.
$object = new MyObject(myCfgObj->getDirective('DIRECTIVE'));
- Static class.
Advantages:
- Can be autoloaded.
Disadvantages:
- Tends to be a bit verbose.
- Can be hard to maintain if a container class is not being used.