PHP code example of selective / config

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

    

selective / config example snippets




use Selective\Config\Configuration;

$config = new Configuration([
    'key1' => [
        'key2' => [
            'key3' => 'value1',
        ]
    ]
]);

// Output: value1
echo $config->getString('key1.key2.key3');

use Selective\Config\Configuration;

// ...

return [
    // Application settings
    Configuration::class => function () {
        return new Configuration(

// Database settings
$settings['db'] = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'username' => 'root',
    'database' => 'test',
    'password' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'flags' => [
        PDO::ATTR_PERSISTENT => false,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ],
];

use Selective\Config\Configuration;
use PDO;

return [
    // ...

    PDO::class => static function (ContainerInterface $container) {
        $config = $container->get(Configuration::class);

        $host = $config->getString('db.host');
        $dbname =  $config->getString('db.database');
        $username = $config->getString('db.username');
        $password = $config->getString('db.password');
        $charset = $config->getString('db.charset');
        $flags = $config->getArray('db.flags');
        $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";

        return new PDO($dsn, $username, $password, $flags);
    },

    // ...

];

$settings['module'] = [
    'key1' => 'my-value',
];



namespace App\Domain\User\Service;

use Selective\Config\Configuration;

final class Foo
{
    private $config;

    public function __construct(Configuration $config)
    {
        $this->config = $config;
    }

    public function bar()
    {
        $myKey1 = $this->config->getString('module.key1');
        
        // ...
    }
}