PHP code example of davidlienhard / config

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

    

davidlienhard / config example snippets


 declare(strict_types=1);
use DavidLienhard\Config\Config;

try {
    $config = new Config("path/to/config");
} catch (\Throwable $t) {
    echo "unable to setup config";
    exit(1);
}

 declare(strict_types=1);

echo $config->get("system", "name");
/* test */

echo $config->get("system", "list1", "key1");
/* value1 */

 declare(strict_types=1);

echo $config->getAsString("system", "name");
/*
 if the value exists and is not an array, it will return a string in any case
 if the value is an array, this will throw an exception
 if the value does not exist this will return null
*/

 declare(strict_types=1);

print_r($config->get("system", "list1"));
/*
    Array
    (
        [key1] => value1
        [key2] => value2
        [key3] => value3
        [key4] => value4
    )
*/

 declare(strict_types=1);

print_r($config->get("system", "list2"));
/*
    Array
    (
        [0] => value1
        [1] => value2
        [2] => value3
        [3] => value4
    )
*/

 declare(strict_types=1);

var_dump($config->get("system", "doesnotexist"));
/* NULL */

 declare(strict_types=1);

var_dump($config->get("doesnotexist"));
/* throws \Exception */

 declare(strict_types=1);

try {
    $config->registerParser(\your\custom\parser::class);
} catch (ConfigException $e) {
    die("unable to register custom parser");
}