PHP code example of xeriab / konfig

1. Go to this page and download the library: Download xeriab/konfig 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/ */

    

xeriab / konfig example snippets


use Exen\Konfig\Konfig;

// Load a single file
$config = Konfig::load('konfig.json');
$config = new Konfig('konfig.json');

// Load values from multiple files
$config = new Konfig(['konfig.json', 'konfig.xml']);

// Load all supported files in a directory
$config = new Konfig(__DIR__ . '/konfig');

// Load values from optional files
$config = new Konfig(['konfig.dist.json', 'konfig.json']);

// Get value using key
$debug = $config->get('debug');

// Get value using nested key
$secret = $config->get('security.secret');

// Get a value with a fallback
$ttl = $config->get('app.timeout', 3000);

// Get value using a simple key
$debug = $config['debug'];

// Get value using a nested key
$secret = $config['security.secret'];

// Get nested value like you would from a nested array
$secret = $config['security']['secret'];

// Get all values
$data = $config->all();

$config = Konfig::load('konfig.json');

// Sample value from our konfig file
assert($config['secret'] == '123');

// Update konfig value to something else
$config['secret'] = '456';

// Reload the file
$config = Konfig::load('konfig.json');

// Same value as before
assert($config['secret'] == '123');

// This will fail
assert($config['secret'] == '456');


use Exen\Konfig\AbstractKonfig;

class MyKonfig extends AbstractKonfig
{
    protected function getDefaults()
    {
        return [
            'host' => 'localhost',
            'port' => 80,
            'servers' => [
                'host1',
                'host2',
                'host3',
            ],
            'app' => [
                'name' => 'konfig',
                'secret' => 'secret',
            ],
        ];
    }
}