dev-master
9999999-devConfig objects instead of arrays
MIT
The Requires
The Development Requires
1.0.0-rc1
1.0.0.0-RC1Config objects instead of arrays
MIT
The Requires
The Development Requires
Config objects instead of arrays
You should use objects instead of arrays because:, (*1)
InstanceConfigTrait
So instead of doing something like, (*2)
class Foo(array $config);
do this, (*3)
class Foo(FooConfig $config);
Create your configuration object:, (*4)
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:, (*5)
$config = new FooConfig(); $config->setBar('some-value); class Foo { protected $config; public function __construct(FooConfig $config) { $this->config = $config; } } $foo = new Foo($config); ```` ### Migrating arrays to objects For a soft migration path you can still do this: ```php 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:, (*6)
$config = new Config(); $config['arrayaccess'] = 'value'; echo $config['arrayaccess'];
Or you can simply get the whole config as array by calling:, (*7)
$configArray = $config->toArray();
Copyright 2013 - 2017 Florian KrÀmer, (*8)
Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file., (*9)
Config objects instead of arrays
MIT
Config objects instead of arrays
MIT