PHPackages                             elbucho/config - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. elbucho/config

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

elbucho/config
==============

Multiple file format object-oriented config system

v1.0.4(7y ago)11542MITPHPPHP &gt;=7.1

Since Sep 13Pushed 7y ago1 watchersCompare

[ Source](https://github.com/elbucho/config)[ Packagist](https://packagist.org/packages/elbucho/config)[ RSS](/packages/elbucho-config/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (3)Dependencies (5)Versions (5)Used By (2)

elbucho/config
==============

[](#elbuchoconfig)

This project provides an object-oriented configuration system for all of your config files. This includes support for the following file types:

- INI files
- PHP files (return array())
- JSON files
- XML files
- YAML files

Usage
-----

[](#usage)

The Config class is instantiated with an array of key =&gt; value pairs. Nested keys are instantiated as separate Config classes.

### Retrieving values

[](#retrieving-values)

Keys can be accessed either through the magic \_\_get() method, or via the get() method. The get() method supports dotted notation (see examples below).

`$config = new Elbucho\Config\Config(array('foo' => array('bar' => 1)));`

`$config->foo->bar == 1`

`$config->get('foo.bar') == 1`

`$config->get('foo')->bar == 1`

`$config->get('foo')->get('bar') == 1`

When using the magic \_\_get() method, if a key does not exist, the Config class will return `false`. When using the get() method, you can specify the return value if the key does not exist.

`$config->foo->bar1 == false`

`$config->get('foo.bar1', 2) == 2`

`$config->foo->get('bar1', 'missing') == 'missing'`

### Setting values

[](#setting-values)

You can set values by using either the magic \_\_set() method, or by using the set() method:

`$config->foo->bar = 2`

`$config->set('foo.bar', 2)`

`$config->foo->set('bar', 2)`

If you use the set() method, and specify a path that doesn't currently exist, that path will be created:

`$config->get('foo.bar.foobar') == false`

`$config->set('foo.bar.foobar.asdf', 'fdsa')`

### Un-setting values

[](#un-setting-values)

You can unset values either by using the magic \_\_unset() method, or by using remove():

`unset($config->foo->bar)`

`$config->remove('foo.bar')`

`$config->foo->remove('bar')`

### Finding whether a key exists

[](#finding-whether-a-key-exists)

You can determine whether a key has been set via either the magic \_\_isset() method, or with exists():

`isset($config->foo->bar)`

`$config->exists('foo.bar')`

`$config->foo->exists('bar')`

### Returning as an array

[](#returning-as-an-array)

You can return the Config object as an array by using the toArray() method:

`$config->toArray() == ['foo' => ['bar' => 1]]`

### Appending values

[](#appending-values)

If you wish to merge two Config classes, you can use the append function:

```
$config1 = new Config(array('foo' => array('bar' => 1)));
$config2 = new Config(array('foo' => array('baz' => 2)));
$config1->append($config2);

```

`$config1->toArray() == ['foo' => ['bar' => 1, 'baz' => 2]]`

File loaders
------------

[](#file-loaders)

You can load a configuration file into a Config object via the use of one of the Loader classes:

File TypeLoader ClassINI`Elbucho\Config\Loader\File\IniFileLoader`JSON`Elbucho\Config\Loader\File\JsonFileLoader`PHP`Elbucho\Config\Loader\File\PhpFileLoader`XML`Elbucho\Config\Loader\File\XmlFileLoader`YAML/YML`Elbucho\Config\Loader\File\YamlFileLoader`To load a file and create a Config-compatible array from the values stored therein, do this:

```
$loader = new YamlFileLoader();
$config = new Config($loader->load('/path/to/config.yaml'));

```

Directory Loader
----------------

[](#directory-loader)

You can also load all of the files in a given directory with the directory loader:

```
$loader = new Elbucho\Config\Loader\DirectoryLoader();
$config = new Config($loader->load('/path/to/config/directory'));

```

File names (minus the extensions) will be listed as keys in the $config object. For example, let's assume this file exists in your config directory as "database.yml":

```
host:   localhost
port:   3306
dbname: test
user:   test_user
pass:   test_password

```

When you run the code above to import this file, here is how your config object will look:

```
$config->toArray() == [
    'database'  => [
        'host'      => 'localhost',
        'port'      => 3306,
        'dbname'    => 'test',
        'user'      => 'test_user',
        'pass'      => 'test_password'
    ]
]

```

### Environment Overwriting

[](#environment-overwriting)

In many cases, you will want to have a set of config files that are applicable for all environments, and will want to overwrite specific keys with environment-specific values.

For example, let's say that we have a config folder format like this:

```
/config
    /environment
        /live
            database.yml
        /test
            database.yml
    database.yml
    framework.yml

```

In this example, we want the keys that are common to all environments in `/config/database.yml`and `/config/framework.yml` loaded, but we want to overwrite certain ones with the values in `/config/environment/live` since we're in the live environment. Here's how you would do that:

```
$environment = 'live';
$configPath = '/config';
$environmentPath = $configPath . '/environment/' . $environment;

$loader = new Elbucho\Config\Loader\DirectoryLoader();
$config = new Config($loader->load($configPath));
$config->remove('environment');

$config->append(
    new Config($loader->load($environmentPath))
);

```

### Extending the class

[](#extending-the-class)

Each loader must conform to the `Elbucho\Config\LoaderInterface` interface. If you would like to write a custom file loader, it should throw an `Elbucho\Config\InvalidFileException` exception when encountering issues opening or parsing the file.

New loaders can also be registered with the DirectoryLoader via the registerLoader() method:

```
$loader = new Elbucho\Config\Loader\DirectoryLoader();

// Registers the CustomFileLoader() class to handle files with '.xyz' extensions
$loader->registerLoader(
    new CustomFileLoader(),
    'xyz'
);

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~24 days

Total

4

Last Release

2730d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/93a0e79682cd43644eb1812ae1706fb8844b1b595fa571bc5f8854af2a670d1a?d=identicon)[elbucho](/maintainers/elbucho)

---

Top Contributors

[![elbucho](https://avatars.githubusercontent.com/u/4545458?v=4)](https://github.com/elbucho "elbucho (10 commits)")

---

Tags

jsonxmlconfigyamlymlini

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/elbucho-config/health.svg)

```
[![Health](https://phpackages.com/badges/elbucho-config/health.svg)](https://phpackages.com/packages/elbucho-config)
```

###  Alternatives

[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[m1/vars

Vars is a simple to use and easily extendable configuration loader with in built loaders for ini, json, PHP, toml, XML and yaml/yml file types. It also comes with in built support for Silex and more frameworks to come soon.

69124.2k1](/packages/m1-vars)[thewunder/conphigure

Framework Agnostic Configuration Library

3115.9k](/packages/thewunder-conphigure)[davidepastore/slim-config

A slim middleware to read configuration from different files based on hassankhan/config

338.9k1](/packages/davidepastore-slim-config)[phppkg/config

Config manage, load, get. Supports INI,JSON,YAML,NEON,PHP format file

133.5k](/packages/phppkg-config)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
