2017 © Pedro PelĂĄez
 

cakephp-plugin cakephp-config-objects

Config objects instead of arrays

image

burzum/cakephp-config-objects

Config objects instead of arrays

  • Wednesday, July 12, 2017
  • by burzum
  • Repository
  • 2 Watchers
  • 2 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Object Config for CakePHP

Objects!? Why not arrays?

You should use objects instead of arrays because:, (*1)

  • 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, (*2)

class Foo(array $config);

do this, (*3)

class Foo(FooConfig $config);

How to use it

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);
    }
}

Array access

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();

License

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)

The Versions

12/07 2017

dev-master

9999999-dev

Config objects instead of arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

06/07 2017

1.0.0-rc1

1.0.0.0-RC1

Config objects instead of arrays

  Sources   Download

MIT

The Requires

 

The Development Requires