PHP Classes

How to Use a PHP Application Config Class to Load Configuration Values from Multiple JSON Files Using the Package PHP Config: Load configuration values from JSON files

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-08-27 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 58 This week: 3All time: 10,535 This week: 13Up
Version License PHP version Categories
php-config-managemen 1.0MIT/X Consortium ...8Files and Folders, Configuration, PHP 8
Description 

Author

This package can load configuration values from JSON files.

It can load JSON one or more files from a given directory and extract configuration values.

The loaded configuration values are made available via a getter function.

Picture of Cristian Radu
Name: Cristian Radu <contact>
Classes: 4 packages by
Country: Romania Romania
Age: ???
All time rank: 438680 in Romania Romania
Week rank: 10 Up1 in Romania Romania Up

Documentation

PHP App Configuration Class

PHP class for managing configuration throughout an application.

Installation

Use composer to install PHP Config.

composer require kris-ro/php-config

Usage

require YOUR_PATH . '/vendor/autoload.php';

use KrisRo\PhpConfig\Config;

# static call
# providing a path (optional) to a folder here will load all json files in the folder
Config::buildConfig('/absolute/path/to/your/folder/with/json/files');

// magic call for debug and salt entries in the config array
Config::debug();
Config::salt();

// setter call
// set method returns the new value
Config::set('database/username', 'k2'); # returns 'k2'

Config::get('database/username'); # returns 'k2'

Initialize as object

// path here is also optional; if provided all json files in that folder will be loaded
new Config('/absolute/path/to/your/folder/with/json/files');

$this->assertEquals('k2', Config::set('database/username', 'k2'));
// verify new value
$this->assertEquals('k2', Config::get('database/username'));

Load json files in specific order

(new Config())
      ->loadConfigFile(/absolut/path/to/first.json')
      ->loadConfigFile(/absolut/path/to/second.json')
      ->loadConfigFile(/absolut/path/to/nth.json');

(new Config())
      ->setConfigPath('/absolute/path/to/your/folder/with/json/files')
      ->loadConfigFile(first-file-to-load.json')
      ->loadConfigFile(second-file-to-load.json')
      ->loadConfigFile(nth-file-to-load.json');


  Files folder image Files (7)  
File Role Description
Files folder imageconfigs (3 files)
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (7)  /  configs  
File Role Description
  Accessible without login Plain text file config-1.json Data Auxiliary data
  Accessible without login Plain text file config-2.json Data Auxiliary data
  Accessible without login Plain text file config-3.json Data Auxiliary data

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file Config.php Class Class source

  Files folder image Files (7)  /  tests  
File Role Description
  Plain text file ConfigTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:58
This week:3
All time:10,535
This week:13Up