PHPackages                             hassankhan/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. hassankhan/config

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

hassankhan/config
=================

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

3.2.0(1y ago)97513.5M↑10.5%134[16 issues](https://github.com/hassankhan/config/issues)[7 PRs](https://github.com/hassankhan/config/pulls)20MITPHPPHP &gt;=7.4

Since Nov 19Pushed 1y ago33 watchersCompare

[ Source](https://github.com/hassankhan/config)[ Packagist](https://packagist.org/packages/hassankhan/config)[ Docs](http://hassankhan.me/config/)[ RSS](/packages/hassankhan-config/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (35)Used By (20)

Config
======

[](#config)

[![Latest version](https://camo.githubusercontent.com/bf2c09fe22a312b89c5f49fcb8d15f390f9eea9d237dfa38a1483bfbc37cf11b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68617373616e6b68616e2f636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hassankhan/config)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](http://hassankhan.mit-license.org)[![Build Status](https://camo.githubusercontent.com/e27888c1c8723eb78974a1d0e8fbbebc629d28088375a1f30f7369e51f56189f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f68617373616e6b68616e2f636f6e6669672f436f6e74696e756f7573253230496e746567726174696f6e2f6d61737465723f7374796c653d666c61742d737175617265)](https://github.com/hassankhan/config/actions?query=workflow%3A%22Continuous+Integration%22+branch%3Amaster)[![Coverage Status](https://camo.githubusercontent.com/a0035378103701b4b0b2f6437c57020c6c20f76c7441f958499cba19aaa26922/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f68617373616e6b68616e2f636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/hassankhan/config/code-structure)[![Quality Score](https://camo.githubusercontent.com/ec8eb26f45014cb9e813c0cfbf48b2568eb6c3cfc081c5ac7b8d1fbf3405e87c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f68617373616e6b68616e2f636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/hassankhan/config)[![Total Downloads](https://camo.githubusercontent.com/8c201d4e7a3a0932b2771456ce97712454901dd41ae6cf3ef828fdfc206b39a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68617373616e6b68616e2f636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hassankhan/config)[![Gitter](https://camo.githubusercontent.com/82b37b704e2e79c5d50f45f6cf0aab5873f32ffcca6c38864d72b1a66c2ba48c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4749545445522d4a4f494e253230434841542532302545322538362539322d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://gitter.im/hassankhan/config?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

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

Requirements
------------

[](#requirements)

Config requires PHP 7.4+.

> **IMPORTANT:** If you want to use YAML files or strings, require the [Symfony Yaml component](https://github.com/symfony/Yaml) in your `composer.json`.

Installation
------------

[](#installation)

The supported way of installing Config is via Composer.

```
$ composer require hassankhan/config
```

Usage
-----

[](#usage)

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

### Loading files

[](#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 `glob`ed 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

[](#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 = 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

[](#setting-values)

Although Config supports setting values via `set()` or, via the array syntax, **any changes made this way are NOT reflected back to the source files**. By design, if you need to make changes to your configuration files, you have to do it manually.

```
$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');
```

### Saving config

[](#saving-config)

It is possible to save the config back to a file in any of the supported formats except PHP.

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

$ini = $config->toString(new Ini()); // Encode to string if you want to save the file yourself

$config->toFile('config.yaml');
$config->toFile('config.txt', new Serialize()); // you can also force the writer
```

### Using with default values

[](#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' => [
                'host1',
                'host2',
                'host3'
            ],
            'application' => [
                'name'   => 'configuration',
                'secret' => 's3cr3t'
            ]
        ];
    }
}
```

### Merging instances

[](#merging-instances)

You may want merging multiple Config instances:

```
$conf1 = Config::load('conf1.json');
$conf2 = Config::load('conf2.json');
$conf1->merge($conf2);
```

### Examples of supported configuration files

[](#examples-of-supported-configuration-files)

Examples of simple, valid configuration files can be found [here](tests/mocks/pass).

Performance
-----------

[](#performance)

Please note that there is a significant drop in performance with configurations in the `yaml` format: Load configuration was 1ms in both `json` and `php` while the same configuration was 5ms with yaml.

Testing
-------

[](#testing)

```
$ phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email [contact@hassankhan.me](mailto:contact@hassankhan.me?subject=%5BSECURITY%5D%20Config%20Security%20Issue) instead of using the issue tracker.

Credits
-------

[](#credits)

- [Hassan Khan](https://github.com/hassankhan)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

61

—

FairBetter than 99% of packages

Maintenance41

Moderate activity, may be stable

Popularity70

Solid adoption and visibility

Community51

Growing community involvement

Maturity75

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~122 days

Recently: every ~366 days

Total

31

Last Release

525d ago

Major Versions

0.11.2 → 1.0.02018-03-03

1.1.0 → 2.0.02018-10-03

2.2.0 → 3.0.02021-12-30

PHP version history (4 changes)0.0.1PHP &gt;= 5.3.0

0.4.0PHP &gt;=5.3.0

1.0.0PHP &gt;=5.5.9

3.1.0PHP &gt;=7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1781985?v=4)[Hassan Khan](/maintainers/hassankhan)[@hassankhan](https://github.com/hassankhan)

---

Top Contributors

[![hassankhan](https://avatars.githubusercontent.com/u/1781985?v=4)](https://github.com/hassankhan "hassankhan (125 commits)")[![DavidePastore](https://avatars.githubusercontent.com/u/1949364?v=4)](https://github.com/DavidePastore "DavidePastore (83 commits)")[![filips123](https://avatars.githubusercontent.com/u/16626308?v=4)](https://github.com/filips123 "filips123 (31 commits)")[![noodlehaus](https://avatars.githubusercontent.com/u/683976?v=4)](https://github.com/noodlehaus "noodlehaus (19 commits)")[![markdegrootnl](https://avatars.githubusercontent.com/u/3601019?v=4)](https://github.com/markdegrootnl "markdegrootnl (16 commits)")[![nbayramberdiyev](https://avatars.githubusercontent.com/u/23094428?v=4)](https://github.com/nbayramberdiyev "nbayramberdiyev (14 commits)")[![equinoxmatt](https://avatars.githubusercontent.com/u/5101742?v=4)](https://github.com/equinoxmatt "equinoxmatt (7 commits)")[![mp3000mp](https://avatars.githubusercontent.com/u/54006012?v=4)](https://github.com/mp3000mp "mp3000mp (6 commits)")[![8ctopus](https://avatars.githubusercontent.com/u/13252042?v=4)](https://github.com/8ctopus "8ctopus (4 commits)")[![dmleach](https://avatars.githubusercontent.com/u/3705674?v=4)](https://github.com/dmleach "dmleach (4 commits)")[![ashnazg](https://avatars.githubusercontent.com/u/100170?v=4)](https://github.com/ashnazg "ashnazg (3 commits)")[![s4msung](https://avatars.githubusercontent.com/u/58741?v=4)](https://github.com/s4msung "s4msung (3 commits)")[![MPur](https://avatars.githubusercontent.com/u/5575697?v=4)](https://github.com/MPur "MPur (3 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")[![hannesvdvreken](https://avatars.githubusercontent.com/u/1410358?v=4)](https://github.com/hannesvdvreken "hannesvdvreken (2 commits)")[![tomzx](https://avatars.githubusercontent.com/u/188960?v=4)](https://github.com/tomzx "tomzx (2 commits)")[![omniError](https://avatars.githubusercontent.com/u/1885212?v=4)](https://github.com/omniError "omniError (2 commits)")[![o5](https://avatars.githubusercontent.com/u/218562?v=4)](https://github.com/o5 "o5 (1 commits)")[![onanying](https://avatars.githubusercontent.com/u/16074765?v=4)](https://github.com/onanying "onanying (1 commits)")[![raffis](https://avatars.githubusercontent.com/u/2376735?v=4)](https://github.com/raffis "raffis (1 commits)")

---

Tags

jsonconfigurationxmlconfigyamlymliniunframeworkmicrophp

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[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)

PHPackages © 2026

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