Proposal for "Config_Lite"

» Metadata » Status
  • Category: Configuration
  • Proposer: Patrick Engel 
  • License: LGPL
» Description

a simple, lightweight and fast class for ini style configuration files, with the native PHP function "parse_ini_file" under the hood.
Config_Lite is inspired by Pythons ConfigParser.

A "Config_Lite" file consists of global options as "name = value" entries on top and sections, "[section]", followed by "name = value" entries.


The Config_Lite Pear Package:

A Configuration File: `test.ini'

public_key_file =  "~/.ssh/"
debug = yes

lang = "en"

user = "dionysis"
password = "c2oiVnY!f8sf"

Read Configuration file:


require_once 'Config/Lite.php';

$config = new Config_Lite('test.ini');

echo $config->get('db', 'user'); // dionysis
echo $config->get(null, 'public_key_file'); // ~/.ssh/

if (true === $config->getBool(null, 'debug', true)) {
        echo $config;

// read with ArrayAccess
echo $config['db']['password']; // c2oiVnY!f8sf
Save Configuration file:


$config->set('db', 'user', 'JohnDoe')
        ->set('db', 'password', 'd0g1tcVs$HgIn1');

// set with ArrayAccess
$config['public_key_file'] = '~/.ssh/';
$config['general'] = array('lang' => 'de');

// save object to file
Create configuration file:


$config = new Config_Lite('test.ini');
$config->set('db', 'user', 'JohnDoe')
        ->set('db', 'password', 'd0g1tcVs$HgIn1');

// set global bool 'debug'
$config->set(null, 'debug', false);

// save object to file
Alternative file creation with write:


$config = new Config_Lite();
$config->write('test.ini', array(
                        'public_key_file' =>  "~/.ssh/",
                        'general' => array(
                                'lang' => 'fr'
                        'db' => array(
                                'user' => 'dionysis',
                                'password' => 'd0g1tcVs$HgIn1'
global Configuration options (without sections) :


$config->set(null, 'private_key_file', '~/.ssh/id_rsa');
// set with arrayaccess
$config['public_key_file'] = '~/.ssh/';


echo $config->get(null, 'public_key_file');
// get with arrayaccess
echo $config['private_key_file'];

Notes & Limitations

Use getString and setString to save and read Strings with double and single-quotes.
Use getBool if you need a real bool type, eg. for strict equality comparision.
The methods `set' and `get' keep values untouched, but the write method normalize "bool" values to a human readable representation, doublequotes strings and numeric values without any quotes.
File locking is not part of this Class.
No support of comments and multiline strings, because reading with "parse_ini_file" does not support it.
If you want to save a regex, i'd recommend to use "get" with base64_decode and "set" with base64_encode.

Save regex (as global option) base64 encoded :


$config = new Config_Lite('regex-test.ini');

$regex = '/Hello \"(.*?)\"/';
$config->set(null, 'regex', base64_encode($regex));
// save object, here sync to read it back, just to test
// in 'regex-test.ini': regex = "L0hlbGxvIFwiKC4qPylcIi8="
$regex = base64_decode($config->get(null, 'regex'));
if (preg_match($regex, 'Hello "World"!')) {
    printf("matched. regex:%s", $regex);
} else {
    printf("no match found. regex:%s", $regex);

Game MENANG123 merupakan game gacor terpercaya.
» Dependencies » Links
» Timeline » Changelog
  • First Draft: 2010-08-31
  • Proposal: 2010-09-09
  • Call for Votes: 2011-01-16
  • Voting Extended: 1970-01-01
  • Patrick Engel
    [2010-09-19 17:09 UTC]

    - Code beautified, fits PEAR Coding Standard (according to phpcs).

    - throws Config_Lite_Exception, which inherits Exception, updated the unit Tests:

  • Patrick Engel
    [2010-09-24 11:47 UTC]

    • updated the Config_Lite Package (used pear to create it),
    • throws Exception, no dependencies,
    • $ phpunit AllTests.php works,
    • phpcs is still quite,
    • package validated
      • to run the phpunit AllTests.php, i used a "if Condition",

    like PHP_CodeSniffer.

    Is it valid to test if it is installed,
    also in the Package's `Lite.php'?

    Should we better throw an Exception that inherits PEAR_Exception?

  • Patrick Engel
    [2010-12-01 04:28 UTC]

    - draft:
    - added static autoload method
    - added (set|get)String with (add|strip)slashes
    - Config_Lite_Exception is a Interface, and is implemented by Config_Lite_Exceptions which inherits SPL Excpetions,

  • Patrick Engel
    [2011-01-07 23:36 UTC]

    - added suggested global options with null by cw,
    - updated the examples, tests and docblocks,
    - also validated and updated the Pear package (with the build script of the gitrepo)

  • Thies C. Arntzen
    [2024-01-18 18:02 UTC]