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

 
composer 
 
class MyAppConfig extends \InitPHP\Config\Classes
{
    public $url = 'http://lvh.me';
    
    public $name = 'LocalHost';
    
    public $db = [
        'host'  => 'localhost',
        'user'  => 'root'
    ];
    
    // ...
}
 
$config = new MyAppConfig();

echo $config->get('url'); 
// Output : "http://lvh.me"

echo $config->get('details', 'Not Found'); 
// Output : "Not Found"

echo $config->get('db.host');
// Output : "localhost"

if($config->has('name')){
    echo $config->get('name');
    // Output : "LocalHost"
}
 
public function setClass(string|object $classOrObject): self;
 
namespace App\Config;

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

class Database 
{
    public $host = 'localhost';
}
 
use \InitPHP\Config\Config;

// Class
Config::setClass(\App\Config\AppConfig::class);

// or Object
Config::setClass(new \App\Config\Database());

Config::get('appconfig.url');

Config::get('database.host');
 
public function setArray(?string $name, array $assoc = []): self;
 
use \InitPHP\Config\Config;

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


Config::get('site.url');
Config::get('site.db.host', '127.0.0.1');
Config::get('site.db.user', 'root');
 
public function setFile(?string $name, string $path): self;
 
 
return [
    'HOST'  => 'localhost',
    'USER'  => 'root',
    'PASS'  => '',
    'NAME'  => 'database'
];
 
use \InitPHP\Config\Config;

Config::setFile('DB', __DIR__ . '/public_html/db_config.php');

// Usage : 
Config::get('db.host');
 
public function setDir(?string $name, string $path, array $exclude = []): self;
 
 
return [
    'HOST'  => 'localhost',
    'USER'  => 'root',
    'PASS'  => '',
    'NAME'  => 'database'
];
 
 
return [
    'URL'   => 'http://lvh.me',
    // ...
];
 
use \PHPConfig\Config;

Config::setDir('app', __DIR__ . '/public_html/config/');

// Usage : 
Config::get('app.site.url');
Config::get('app.db.host');