PHP code example of miladrahimi / phpconfig

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

    

miladrahimi / phpconfig example snippets


use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\ArrayRepository;

$data = [
    'name' => 'MyProject',
    'mysql' => [
        'hostname' => '127.0.0.1',
        'username' => 'root',
        'password' => 'secret',
        'database' => 'my_database'
    ]
];

$repository = new ArrayRepository($data);
$config = new Config($repository);

$name = $config->get('name'); // "MyProject"
$mysql_username = $config->get('mysql.username', 'root'); // "root"
$mysql_password => $config->get('mysql.password', ''); // "secret"
$locale = $config->get('locale', 'en'); // "en" (Default)

// Runtime config setting/manipulating
$config->set('locale', 'fa');

$locale = $config->get('locale', 'en'); // "fa"




return [
    'name' => 'MyProject',
    'mysql' => [
        'hostname' => '127.0.0.1',
        'username' => 'root',
        'password' => 'secret',
        'database' => 'my_database'
    ]
];

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\FileRepository;

$repository = new FileRepository('config.php');
$config = new Config($repository);

$mysql_username = $config->get('mysql.username');


    
return [
    'name' => 'MyProject',
    'locale' => [
        'code' => 'fa',
        'name' => 'Farsi (Persian)',
    ],
    'url' => 'https://example.com'
];



return [
    'hostname' => '127.0.0.1',
    'username' => 'root',
    'password' => 'secret',
    'database' => 'my_database'
];

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\DirectoryRepository;

$config = new Config(new DirectoryRepository('configs'));

$app_locale_code = $config->get('app.locale.code', 'en');
$mysql_username = $config->get('mysql.username', 'default-username');

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\JsonFileRepository;

$config = new Config(new JsonFileRepository('config.json'));

$app_locale_code = $config->get('app.locale.code', 'en'); // "fa"

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\JsonDirectoryRepository;

$config = new Config(new JsonDirectoryRepository('configs'));

$app_locale_code = $config->get('app.locale.code', 'en'); // "fa"
$mysql_username = $config->get('mysql.username', 'default-username');


    
namespace MiladRahimi\PhpConfig\Repositories;

interface Repository
{
    /**
     * Get configuration data
     *
     * @return array
     */
    public function getData(): array;
}

Project
- index.php
- configs
- - app.php
- - mysql.php

Project
- index.php
- configs
- - app.json
- - mysql.json