PHP code example of simoneddy / config

1. Go to this page and download the library: Download simoneddy/config library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simoneddy / config example snippets




// Using the Config constructor. This method  \Eddy\Config\Config($values = []);

// Using the MutableConfig constructor. This method allows config values be supplied as the
// constructors $values argument, but can be modified after construction.
$config = new \Eddy\Config\MutableConfig($values = []);

// Using the Config objects static factory method
$config = \Eddy\Config\Config::fromPath(__DIR__ . '/config');

// Using the MutableConfig objects static factory method
$config = \Eddy\Config\MutableConfig::fromPath(__DIR__ . '/config');

// Using the ConfigFactory directly
$config = \Eddy\Config\ConfigFactory::fromPath(__DIR__ . '/config', $isMutable = false);

// Using a new ConfigFactory to create an immutable Config object
$config = (new \Eddy\Config\ConfigFactory())->createImmutable(__DIR__ . '/config');

// Using a new ConfigFactory to create a mutable MutableConfig object
$config = (new \Eddy\Config\ConfigFactory())->createMutable(__DIR__ . '/config');

// config directory contains the file 'test.php' which returns an array:
// ['isTest' => true]
$config = \Eddy\Config\Config::fromPath(__DIR__ . '/config');

// values can be retrieved with dot notation, where the filename is the parent
// key:
var_dump($config['test.isTrue']);

$config = new \Eddy\Config\Config([
    'someKey' => 'some value',
    'someMoreKeys' => [
        'anotherKey' => 'another value'
    ]
]);

echo $config['someKey']; // 'some value'

// Using dot notation to access nested keys:
var_dump(isset($config['someMoreKeys.anotherKey'])); // true

echo $config->get('someKey'); // 'some value'

var_dump($config->has('someMoreKeys.anotherKey')); // true

// MutableConfig can be modified after construction
$config = new \Eddy\Config\MutableConfig([
    'someKey' => 'some value',
    'someMoreKeys' => [
        'anotherKey' => 'another value'
    ]
]);

// Now offsetSet works
$config['anotherKey'] = 'another value';

// Which is the same as using the set method:
$config->set('andAnother', 'yet another value');
var_drump(isset($config['anotherKey'])); // true

// OffsetUnset works too!
unset($config['someKey']);

// Which is using the remove method:
$config->remove('andAnother');
var_dump($config->has('someKey')); // false
var_dump($config->has('andAnother')); // false

$config->set('parent.nested.nestedTwice.nestedThrice', 'nested value');

// returns an array: ['nested' => ['nestedTwice' => ['nestedThrice' => 'nested value']]]
var_dump($config->get('parent'));

unset($config['parent.nested.nestedTwice']);

// returns an array: ['nested' => []]
var_dump($config->get('parent'));

$config->set('parent.nested.nestedTwice', 'nested value');

// returns an array: ['nested' => ['nestedTwice' => 'nested value']]
var_dump($config->get('parent'));

$config->overwrite('parent.newThing', 'new value');

// returns ['newThing' => 'new value']
// 'nestedTwice' is no longer set as 'parent' has been overwritten
var_dump($config['parent']);