PHP code example of popphp / pop-config

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

    

popphp / pop-config example snippets


use Pop\Config\Config;

$config = new Config(['foo' => 'bar']);

$foo = $config->foo;
// OR
$foo = $config['foo'];

use Pop\Config\Config;

$config = new Config(['database' => ['host' => 'localhost', 'port' => 5432]]);

$host = $config['database.host'];
// OR
$host = $config->{'database.host'};

use Pop\Config\Config;

$config = new Config(['database' => ['host' => 'localhost']], true);

$config['database.port'] = 5432;
// $config->toArray() is now ['database' => ['host' => 'localhost', 'port' => 5432]]

unset($config['database.host']);
// removes just the 'host' key, leaving ['database' => ['port' => 5432]]

use Pop\Config\Config;

$config = new Config(['foo' => 'bar'], true);
$config->foo = 'New Value';

$config->changesAllowed(); // true

use Pop\Config\Config;

$config = new Config($configData, true);
$config->merge($newData);

$config->merge($newData, true);

$config->mergeFromData('/path/to/other-config.json');
// OR, preserving existing values on collision
$config->mergeFromData('/path/to/other-config.json', true);

use Pop\Config\Config;

$config = new Config($configData);
$data   = $config->toArray();

use Pop\Config\Config;

$config = new Config($configData);

$arrayObject       = $config->toArrayObject();       // Pop\Utils\ArrayObject
$nativeArrayObject = $config->toArrayObject(true);   // native \ArrayObject, ARRAY_AS_PROPS

use Pop\Config\Config;

$config = Config::createFromData('/path/to/config.ini');

// $value equals 1
$value = $config->foo['bar'];
// OR
$value = $config['foo']['bar'];

use Pop\Config\Config;

$config = new Config($configData);
echo $config->render('json');

use Pop\Config\Config;

$config = new Config($configData);
$config->writeToFile('/path/to/config.json');

use Pop\Config\Config;
use Pop\Config\ParseException;

try {
    $config = Config::createFromData('/path/to/config.yml');
} catch (ParseException $e) {
    // handle a missing file or malformed content specifically
}

use Pop\Config\Config;
use Pop\Config\ChangesNotAllowedException;

$config = new Config(['foo' => 'bar']); // changes not allowed (the default)

try {
    $config->foo = 'baz';
} catch (ChangesNotAllowedException $e) {
    // handle the immutability violation specifically
}