PHPackages                             nabeghe/confix - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. nabeghe/confix

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nabeghe/confix
==============

Static-architecture PHP configuration library. Simple, file-backed, class-based configuration with parent/child partitioning and dot-notation support.

10PHP

Pushed todayCompare

[ Source](https://github.com/nabeghe/confix-php)[ Packagist](https://packagist.org/packages/nabeghe/confix)[ RSS](/packages/nabeghe-confix/feed)WikiDiscussions main Synced today

READMEChangelog (1)DependenciesVersionsUsed By (0)

Confix
======

[](#confix)

> **Static-architecture PHP configuration library.**
> Simple, file-backed, class-based configuration with parent/child partitioning and dot-notation support.

[![PHP](https://camo.githubusercontent.com/62aeff98201d8b5641823c0f650139b2e355414fe60cbb72fe705f1c6c55505d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344382e332d626c7565)](https://www.php.net/)[![License: MIT](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](LICENSE)

---

Table of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
- [Installation](#installation)
- [Core Concepts](#core-concepts)
    - [Standalone Config (own file)](#standalone-config-own-file)
    - [Child Config (shared file)](#child-config-shared-file)
    - [Dynamic Method Calls](#dynamic-method-calls)
    - [Nested Dot-Notation](#nested-dot-notation)
    - [Custom Validation](#custom-validation)
- [Constants Reference](#constants-reference)
- [API Reference](#api-reference)
    - [Reading Values](#reading-values)
    - [Writing Values](#writing-values)
    - [Removing Values](#removing-values)
    - [Bulk Data Operations](#bulk-data-operations)
    - [File Persistence](#file-persistence)
    - [Overridable Hooks](#overridable-hooks)
- [Running Tests](#running-tests)
- [License](#license)

---

Overview
--------

[](#overview)

Confix is designed with the goal that your configuration system should not require instantiating a class; it works in a fully static manner. You can have static configuration classes whose data source is a file; a file that returns the configuration as an array. Each class can have its own dedicated config file, or it can be a section within a parent config file.

In fact, two types of classes can be implemented using Confix:

1. Parent classes (Standalone configs) that have their own dedicated config file. For example, `AppSettings`, whose file could be `app.php`.
2. Child (nested) classes that are linked to a parent class. For example, `DatabaseSettings`, which can be linked to `app.php` and whose configuration is stored within a section (key) named `database`.

The purpose of having dedicated classes for configs is to use dynamic static methods, handled by \_\_`callStatic`; for getting and setting config keys, so there is no need to write keys as raw strings. There is also no need to actually implement the static methods; it is sufficient to declare them in the class docblock as `@method` annotations to assist the IDE. That said, methods for accessing keys as plain strings or via dot-notation are also available.

---

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

[](#installation)

```
composer require nabeghe/confix
```

Requires **PHP ≥ 8.3**.

---

Core Concepts
-------------

[](#core-concepts)

### Base Config class

[](#base-config-class)

By default, Confix uses the config directory in your project root. It is recommended to configure this path yourself, even if you do not intend to change it. Therefore, you should have a base class, and all other config classes should extend this class instead of extending Confix directly. So:

```
use Nabeghe\Confix\Confix;

abstract class Settings extends Confix
{
    #[Override]
    protected static function getDirectory(): string
    {
        // TODO: Return the path to the config files directory without a trailing slash.
    }
}
```

You can also override methods such as `mkdir`, `filePutContents`, and `varExport` to implement your own logic. I recommend using `Symfony\Component\VarExporter` for `varExport`, and `Symfony\Component\Filesystem\Filesystem` for `mkdir` and `filePutContents`.

### Standalone Config (own file)

[](#standalone-config-own-file)

A standalone config declares `protected static array $_;` which signals that it owns its own config file.

```
use Nabeghe\Confix\Confix;

/**
 * @method static string name(string $value = 0)
 * @method static bool debug(bool $value = 0)
 * @method static string version(string $value = 0)
 */
class AppConfig extends Confix
{
    const string NAME = 'app'; // → config/app.php

    const array DEFAULTS = [
        'name'    => 'My Application',
        'debug'   => false,
        'version' => '1.0.0',
    ];

    protected static array $_; // declares file ownership
}
```

The directory defaults to `/config/`. Override `getDirectory()` to customise it.

---

### Child Config (shared file)

[](#child-config-shared-file)

A child config sets `PARENT` to point at another config class. It lives as a **keyed section** inside the parent's file; no separate file is created.

```
/**
 * @method static string driver(string $value = 0)
 * @method static int ttl(int $value = '')
 */
class CacheConfig extends Confix
{
    const string NAME     = 'cache'; // key inside app.php

    const ?string PARENT  = AppConfig::class;

    const array DEFAULTS = [
        'driver' => 'file',
        'ttl'    => 600,
    ];

    // No protected static array $_ here!
}
```

The resulting `app.php` file will look like:

```
