Wallogit.com
2017 © Pedro Peláez
A package for reading and writing config file
A PHP package for reading and setting configuration fast and easy., (*1)
$ composer require senhung/config-read-write
Create a file and input the configurations, (*2)
For example:, (*3)
# Some Comments APP_NAME=config-read VERSION=2.0.0
Configruation::initializeConfigs(
[string $configFilePath [, bool $absolutePath [, string $separator]]]
): void
$configFilePath: the location your config file is placed (default: '.env'), (*4)
$absolutePath: the path defined is absolute path or relative path (default: true), (*5)
$separator: the separator between config keys and values (default: '='), (*6)
Add the following code in your program's main entry if you want to specify a different config file name or separator other than the default ones, (*7)
<?php
require_once 'vendor/autoload.php';
use Senhung\Config\Configuration;
/* Initialize config array in Configuration class */
Configuration::initializeConfigs('config_file_path', true, 'separator');
Note: you don't need to have this file if you are using .env as your config path and = as your separator., (*8)
Use # in config file to comment a line, (*9)
# This is a comment, will not be read by the tool OTHER_CONFIG=will-be-read
Note: Set config will overwrite comments and empty lines in original config file, (*10)
Configuration::read('<config-you-want-to-read>');
Configuration::set('<config-you-want-to-write>', '<change-to>');
Note: Set config will overwrite comments and empty lines in original config file, (*11)
# App Configs APP_NAME=config-read VERSION=2.0.0
<?php
require_once 'vendor/autoload.php';
use Senhung\Config\Configuration;
/* Read config APP_NAME */
echo Configuration::read('APP_NAME') . "\n";
/* Read config VERSION */
echo Configuration::read('VERSION') . "\n";
/* Set APP_NAME to config-write */
Configuration::set('APP_NAME', 'config-write');
/* Read APP_NAME again to see the changes */
echo Configuration::read('APP_NAME') . "\n";
Output:, (*12)
config-read 2.0.0 config-write