This class reads configuration values stored in a PHP file and makes them available as properties.
Install via Composer
$ composer require web6/config
Create a PHP file which will contain the configuration. This file must be readable by the server.
<?php
// The configuration is stored in a simple PHP array
$config = array();
$config['title'] = 'Lorem Ipsum sit dolor amet';
// IMPORTANT : return the configuration!
return $config;
Using a PHP array for the configuration offers a lot of flexibility and a great readability. Some examples are loctaed at the end of this file.
Configure autoloading by including Composer's generated file :
include_once('vendor/autoload.php');
To read configuration values, instanciate the W6\Config\Reader
class with the configuration file path.
var $config = new \W6\Config\Reader( '/path/to/config.php' );
print_r($config->environment);
print_r($config->debug);
print_r($config->debug['level']);
Samples demonstrating the possibilities offered by PHP configuration.
<?php
$conf = array();
/**
* SECTION : ENVIRONMENT
* Defines the environment
* Possible choices :
* - development
* - production
*/
$conf['environment'] = 'development';
/**
* SECTION : DEBUG
* Debugging options
*/
$conf['debug'] = array();
/**
* Debug level
* 0 - No messages
* 1 - Show messages
* 2 - Show messages + backtrace
*/
$conf['debug']['level'] = $conf['environment'] == 'development' ? 2 : 0;
/**
* SECTION : USER
* Include other configuration files
*/
$conf['user'] = include( './config/user.php' );
// Important, return the configuration array
return $conf;