PHP code example of initphp / config

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

    

initphp / config example snippets


use InitPHP\Config\Classes;

final class MyAppConfig extends Classes
{
    public string $url  = 'http://lvh.me';
    public string $name = 'LocalHost';

    /** @var array<string, string> */
    public array $db = [
        'host' => 'localhost',
        'user' => 'root',
    ];
}

$config = new MyAppConfig();

$config->get('url');                 // "http://lvh.me"
$config->get('db.host');             // "localhost"
$config->get('details', 'Not Found'); // "Not Found"

if ($config->has('name')) {
    $config->get('name');            // "LocalHost"
}

use InitPHP\Config\Library;

$config = new Library();

$config->set('site.url', 'http://lvh.me')
       ->set('site.db.host', 'localhost');

$config->get('site.url');     // "http://lvh.me"
$config->get('SITE.DB.HOST'); // "localhost" (case-insensitive)

use InitPHP\Config\Config;

Config::setArray('site', ['url' => 'http://lvh.me']);

Config::get('site.url'); // "http://lvh.me"
Config::has('site.url'); // true

Config::reset(); // discard the shared instance (useful in tests)

public function setArray(?string $name, array $assoc = []): self;

Config::setArray('site', [
    'url' => 'http://lvh.me',
    'db'  => ['host' => 'localhost', 'user' => 'db_user'],
]);

Config::get('site.url');     // "http://lvh.me"
Config::get('site.db.host'); // "localhost"

public function setFile(?string $name, string $path): self;

// config/db.php
return [
    'HOST' => 'localhost',
    'USER' => 'root',
];

Config::setFile('db', __DIR__ . '/config/db.php');

Config::get('db.host'); // "localhost"  (keys are case-insensitive)

public function setDir(?string $name, string $path, array $exclude = []): self;

// config/db.php   -> returns ['HOST' => 'localhost']
// config/site.php -> returns ['URL'  => 'http://lvh.me']

Config::setDir('app', __DIR__ . '/config', ['secrets']);

Config::get('app.db.host'); // "localhost"
Config::get('app.site.url'); // "http://lvh.me"

public function setClass(string|object $classOrObject): self;

namespace App\Config;

class AppConfig
{
    public string $url = 'http://lvh.me';
}

class Database
{
    public string $host = 'localhost';
}

Config::setClass(\App\Config\AppConfig::class); // by class name
Config::setClass(new \App\Config\Database());   // by instance

Config::get('appconfig.url'); // "http://lvh.me"
Config::get('database.host'); // "localhost"

$config = new Library();
$config->set('db.host', 'localhost')->set('db.user', 'root');

$config->db->host; // "localhost"
$config->db->user; // "root"

use InitPHP\Config\Exceptions\ConfigException;

try {
    Config::setFile('db', '/path/to/missing.php');
} catch (ConfigException $e) {
    // "Configuration file "/path/to/missing.php" was not found."
}