Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
This branch is 4 commits ahead, 101 commits behind hassankhan:develop.

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Latest version Software License Build Status Coverage Status Quality Score Total Downloads Gitter

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files and strings.


Config requires PHP 5.5.9+.

IMPORTANT: If you want to use YAML files or strings, require the Symfony Yaml component in your composer.json.


The supported way of installing Config is via Composer.

$ composer require hassankhan/config


Config is designed to be very simple and straightforward to use. All you can do with it is load, get, and set.

Loading files

The Config object can be created via the factory method load(), or by direct instantiation:

use Noodlehaus\Config;
use Noodlehaus\Parser\Json;

// Load a single file
$conf = Config::load('config.json');
$conf = new Config('config.json');

// Load values from multiple files
$conf = new Config(['config.json', 'config.xml']);

// Load all supported files in a directory
$conf = new Config(__DIR__ . '/config');

// Load values from optional files
$conf = new Config(['config.dist.json', '?config.json']);

// Load a file using specified parser
$conf = new Config('configuration.config', new Json);

Files are parsed and loaded depending on the file extension or specified parser. If the parser is specified, it will be used for all files. Note that when loading multiple files, entries with duplicate keys will take on the value from the last loaded file.

When loading a directory, the path is globed and files are loaded in by name alphabetically.

Warning: Do not include untrusted configuration in PHP format. It could contain and execute malicious code.

Loading string

Configuration from string can be created via the factory method load() or by direct instantiation, with argument $string set to true:

use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
use Noodlehaus\Parser\Yaml;

$settingsJson = <<<FOOBAR
  "application": {
    "name": "configuration",
    "secret": "s3cr3t"
  "host": "localhost",
  "port": 80,
  "servers": [

$settingsYaml = <<<FOOBAR
    name: configuration
    secret: s3cr3t
host: localhost
port: 80
- host1
- host2
- host3


$conf = Config::load($settingsJson, new Json, true);
$conf = new Config($settingsYaml, new Yaml, true);

Warning: Do not include untrusted configuration in PHP format. It could contain and execute malicious code.

Getting values

Getting values can be done in three ways. One, by using the get() method:

// Get value using key
$debug = $conf->get('debug');

// Get value using nested key
$secret = $conf->get('security.secret');

// Get a value with a fallback
$ttl = $conf->get('app.timeout', 3000);

The second method, is by using it like an array:

// Get value using a simple key
$debug = $conf['debug'];

// Get value using a nested key
$secret = $conf['security.secret'];

// Get nested value like you would from a nested array
$secret = $conf['security']['secret'];

The third method, is by using the all() method:

// Get all values
$data = $conf->all();

Setting values

Although Config supports setting values via set() or, via the array syntax.

You can convert it back to string of file. You could currently only save it to one file and PHP is not supported.

$conf = Config;

$conf[] = [
    'database' => [
        'host' => 'localhost',
        'port' => '3306',
    'app' => [
        'name' => 'config',
        'description' => 'Config Reader and Writer',

$result = $conf->toString(new Json);


If you don't call toString() or toFile(), any changes will NOT reflected back to the source files.

$conf = Config::load('config.json');

// Sample value from our config file
assert($conf['secret'] == '123');

// Update config value to something else
$conf['secret'] = '456';

// Reload the file
$conf = Config::load('config.json');

// Same value as before
assert($conf['secret'] == '123');

// This will fail
assert($conf['secret'] == '456');

Using with default values

Sometimes in your own projects you may want to use Config for storing application settings, without needing file I/O. You can do this by extending the AbstractConfig class and populating the getDefaults() method:

class MyConfig extends AbstractConfig
    protected function getDefaults()
        return [
            'host' => 'localhost',
            'port'    => 80,
            'servers' => [
            'application' => [
                'name'   => 'configuration',
                'secret' => 's3cr3t'

Merging instances

You may want merging multiple Config instances:

$conf1 = Config::load('conf1.json');
$conf2 = Config::load('conf2.json');

Examples of supported configuration files

Examples of simple, valid configuration files can be found here.


$ phpunit


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.



The MIT License (MIT). Please see License File for more information.


Config is a lightweight configuration reader and writer that supports PHP, INI, XML, JSON, and YAML (temporary fork)







No packages published


  • PHP 100.0%