You should use objects instead of arrays because:
- An array can't be type checked if it really is the intended config
- It is easy to have typos in config arrays
- The configuration is separated from the class unlike when using
InstanceConfigTrait
- Optional bonus: An array doesn't bring easy validation of config data with it
So instead of doing something like
class Foo(array $config);
do this
class Foo(FooConfig $config);
Create your configuration object:
use Burzum\ObjectConfig\Config;
class FooConfig extends Config {
protected $_defaultConfig = [
// Set your default values here
];
/* Your setter / getter methods go here */
}
Then just use it:
$config = new FooConfig();
$config->setBar('some-value);
class Foo {
protected $config;
public function __construct(FooConfig $config)
{
$this->config = $config;
}
}
$foo = new Foo($config);
For a soft migration path you can still do this:
class Foo {
protected $config;
public function __construct(array $config = [])
{
$this->config = FooConfig::createFromArray($config);
}
}
The Config
class implements \ArrayAccess
. So even when you change the signature of a method to require a specific type of object, your underlying code can still access the config like an array:
$config = new Config();
$config['arrayaccess'] = 'value';
echo $config['arrayaccess'];
Or you can simply get the whole config as array by calling:
$configArray = $config->toArray();
Copyright 2013 - 2017 Florian Krämer
Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.