PHP Configuration System

PHP Configuration System


version 2.0.3! (September 23, 2014)

Warning: version 2 is not backwards-compatible with older versions!
Version 2 allows you to save and create configurations at run-time (on the fly). It includes a video tutorial too!

  • Set values using config->set method
  • Has a new “namespace” concept, which allows for bigger projects and more hierarchical setups.

Version 1.2.4 (June 28, 2014)

  • Fixed a typo in Config.php, line 39 where ‘default’ was spelled ‘defualt’. Thanks damirg!
  • * Completed the example02.php in examples/ dir. It now demonstrates a static facade  :)


Introducing NeatConfig – The simple way to manage the configuration of your application. Supports multiple environments!

  • Store your configurations as simple .php arrays
  • Manage multiple environments with ease
  • Use array dot-notation to get nested config values
  • Includes an example of a static facade
  • Well-documented, clean object-oriented code!

Installation & Basic use (updated for 2.0)

This script is one single class, in 1 file. So it should be fairly easy to get started. However, here’s a basic introduction.

Let’s say you have a website root directory that looks like this:

  • index.php

To use NeatConfig, download the zip file and unzip the src/Afflicto/NeatConfig/ directory to the root of your website, such that your web root looks like this:

  • NeatConfig/
    • Config.php
    • functions.php
  • index.php

To begin using NC, first. require the Config.php class.

require_once 'NeatConfig/Config.php';

First, let’s create our Config instance:

$config = new Afflicto\NeatConfig\Config('config');

The first argument is the path (relative or absolute) to where you want to store configurations. Let’s say ‘config’, simply. Also, create this directory in your web root, such that you now how this:

  • Config/
  • NeatConfig/
    • Config.php
    • functions.php
  • index.php

Ok, time to create some configuration data:

    $config->set('core:database', array('driver' => 'mysql'));

What’s with the core:database and the colon and stuff? NeatConfig 2.0 has a concept of namespaces, which are just an organizational feature. Very useful for larger projects like CMS’s or frameworks etc.

Run your script (with the above code, where we used the set method).
Take a look at the config directory in your web root. Notice that it should have created a ‘default’ directory, in there should be a ‘core’ directory and again in there should be a ‘database.php’ file.

Getting values is just as easy, you can get an entire configuration (database.php) or a specific key in a configuration. Try these:

    $config->get('core:database');//array('driver' => 'mysql')

So, as you can see. NeatConfig uses this ‘dot-notation’ for accessing arrays. Both when setting and getting stuff.

Let’s add another value to our database config. This take, we’ll add an entire array:

    $config->set('core.database.connections', array('mysql' => array(
        'host' => 'localhost',
        'username' => 'myname',
        'password' => '1234',

Now, if you run that script. You’ll see that it just modifies the database.php file (in your config/default/core directory.

That’s a quick introduction to creating, editing and saving data. You can create as many namespaces you have, beyond the “core” we used in this tutorial and you can have as many configuration files (database.php, cache.php, user.php etc) as you want.

Multiple Environments

So far, we only used the ‘default’ namespace. (created automatically for you) All configuration data for the default namespaces are located in config/default.

Multiple environments are handled in such a way where, the ‘default’ namespace acts as a fallback when the environment currently accessed doesn’t provide a value.

I assume you have read the first part of this tutorial (basic usage, above).
Let’s continue from that, we already have NeatConfig in our project and we have some database configuration data in our default environment in the ‘core’ namespace.

When you instantiate the Config instance, the second argument (which is optional and defaults to ‘default’) determines the environment to use for that session. (untill you change it). Let’s begin using a ‘local’ environment (the directory for it, will be created automatically).

    $config = new Afflicto\NeatConfig\Config('config', 'local');

If we do this now:

    echo $config->get('core:database.driver');

We should get “mysql”. This is because, again, other environments will “fallback” to the ‘default’ environment when we try to access something that doesn’t exist. So, we haven’t overriden the core:database.driver value (yet).

Let’s override it, we can override some settings by simply creating data in our config/local directory.
For this example, let’s say that we want the ‘local’ environment to use a different database driver.

Ok, let’s run the following:

    //make sure you have the environment set to 'local' this time.
    //(new Afflicto\NeatConfig\Config('config', 'local'))
    $config->set('core:database.driver', 'nosql');

That should have created a file called database.php in the config/local/core directory.

Now, if you run this:

    echo $config->get('core:database.driver');

While having the environment set to ‘local’, you should get “nosql”.

If we now change the environment back to ‘default’, we’ll get ‘mysql’ again.

Thanks for purchasing, please rate the product and if you have any questions: don’t hesitate to ask!