PHP code example of alex-unruh / dot-notation-config

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

    

alex-unruh / dot-notation-config example snippets


// config/app.php

return [
  'app_name' => 'My App',

  'app_version' => '1.0.0',

  'connection_params' => [
    'host' => 'localhost',
    'dbname' => 'my_database',
    'user' => 'root',
    'password' => '',
    'port' => '3306'
  ]
];

// config/messages.php

return [
  'internal_error' => 'Internal server error',
  400 => 'Bad request'
];

// index.php

use AlexUnruh\Config;

Config::setDir('/config');

// Search data in /config/app.php file
echo Config::get('app.app_name'); // 'My App'
echo Config::get('app.connection_params.host'); // 'localhost'

// Search data in /config/messages.php file
echo Config::get('messages.400'); // 'Bad request'

print_r(Config::get('app')); // Returns all the array data placed in the app file.


// index.php

use AlexUnruh\Config;

$data = [
  'app_name' => 'My App',

  'app_version' => '1.0.0',

  'connection_params' => [
    'host' => 'localhost',
    'dbname' => 'my_database',
    'user' => 'root',
    'password' => '',
    'port' => '3306'
  ]
];

Config::setData('my_data', $data);

echo Config::get('my_data.app_name'); // 'My App'
echo Config::get('my_data.connection_params.host'); // 'localhost'

print_r(Config::get('my_data')); // Returns all the array data placed in the my_data array.


// $my_config_dir = '/my-config-dir'
Config::setDir($my_config_dir);

// $my_array = ['app_name' => 'My App', 'app_version' => '1.0.0']
Config::setData($my_array);

// $my_array_search = 'app.app_name'
// $default_if_key_not_exists = 'My App'
Config::get($my_array_search, $default_if_key_not_exists); // 'My App'

// $my_array_item = 'app.name'
// $my_new_value = 'My New App Name'
Config::set($my_array_item, $my_new_value);

// $my_array_data = 'app'
// $the_key_im_looking_for = 'app_version'
Config::has($my_array_data, $the_key_im_looking_for); // true

// $my_array_item = 'app.app_licence'
// $value_to_append = 'MIT'
Config::append($my_array_item, $value_to_append);

// $my_array_item = 'app'
// $value_to_remove = 'app_licence'
Config::remove($my_array_item, $value_to_remove);