PHP code example of philiprehberger / php-config-loader

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

    

philiprehberger / php-config-loader example snippets


use PhilipRehberger\ConfigLoader\ConfigLoader;

// Load a PHP config file
$config = ConfigLoader::load(__DIR__ . '/config/app.php');

// Load a JSON config file
$config = ConfigLoader::load(__DIR__ . '/config/database.json');

// Load a YAML config file (


return [
    'name' => 'MyApp',
    'debug' => true,
    'version' => '1.0.0',
];

$config->get('credentials.user');          // 'admin'
$config->get('missing.key', 'default');    // 'default'

$config->string('name');          // string, default ''
$config->int('port', 3306);       // int, default 0
$config->bool('debug', false);    // bool, default false
$config->float('rate', 1.0);      // float, default 0.0
$config->array('tags', []);       // array, default []

$config->has('credentials.user');  // true
$config->has('nonexistent');       // false

putenv('DB_HOST=production.example.com');
$config = ConfigLoader::load('config.json');

$config->get('host');     // 'production.example.com'
$config->get('api_key');  // '' (env var not set, no default)

// config/
//   app.php      -> keyed as 'app'
//   database.json -> keyed as 'database'

$config = ConfigLoader::loadDirectory(__DIR__ . '/config');

$config->get('app.name');           // from app.php
$config->get('database.host');      // from database.json

$base = ConfigLoader::load('config/defaults.php');
$local = ConfigLoader::load('config/local.php');

$merged = $base->merge($local);

$config = ConfigLoader::load('config/database.php');
// ['database' => ['host' => 'localhost', 'port' => 3306]]

$flat = $config->flatten();
// ['database.host' => 'localhost', 'database.port' => 3306]

// Use a custom separator
$flat = $config->flatten('/');
// ['database/host' => 'localhost', 'database/port' => 3306]

$config = ConfigLoader::load('config/database.json');

$violations = $config->validate([
    'host' => 'tions as $message) {
        echo $message . PHP_EOL;
    }
}