PHP code example of timdev / typed-config

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

    

timdev / typed-config example snippets


declare(strict_types=1);

// This should already be set in your php.ini, but this library depends on it,
// so you should verify that's set.
ini_set('assert.exception', 1);

// Could come from anywhere.
$configArray = [
    'myapp' => [
        'some-api' => [
            'baseUrl' => 'https://example.com/v1/',
            'timeout' => 30
        ],
        'allowed_ips' => [
            '192.168.0.1',
            '192.168.0.50'
        ],
        'optional_value' => null        
    ]
];

$config = new Config($configArray);

// Methods have narrow return type declarations:
$baseUrl = $config->string('myapp.some-api.baseUrl');
$timeout = $config->int('myapp.some-api.timeout');
$allowed = $config->list('myapp.allowed_ips');

// Attempting to access an undefined value throws.
$port = $config->int('invalid.key');  // Throws \TimDev\TypedConfig\Exception\KeyNotFound

// Note that the main methods do not have nulalble return types.
$optional = $config->bool('myapp.optional_value'); // Also throws!

// Use ->nullable to access valeus that are defined, but might be null:
$optional = $config->nullable->bool('myapp.optional_value');