Skip to content

The Library Object

Muhammet Şafak edited this page May 29, 2026 · 1 revision

The Library Object

InitPHP\Config\Library is the package's main entry point: an injectable object that holds a configuration tree and knows how to populate it from several sources. Inject it where you need isolated, testable configuration — as opposed to the global Config facade.

use InitPHP\Config\Library;

$config = new Library();                 // empty
$config = new Library(['db' => []]);     // seeded from an array

The read/write surface

Library implements ConfigInterface:

$config->set('app.name', 'InitPHP'); // returns $this — chainable
$config->get('app.name');            // 'InitPHP'
$config->get('app.locale', 'en');    // 'en' (default — key absent)
$config->has('app.name');            // true
$config->remove('app.name');         // returns $this
$config->all();                      // the whole tree as an array

See Keys & Dotted Paths for how keys are parsed.

Loaders

Library can import configuration from arrays, PHP files, directories, and class/object properties. Each loader returns $this, so they chain with everything else:

$config
    ->setArray('site', ['url' => 'http://lvh.me'])
    ->setFile('db', __DIR__ . '/config/db.php')
    ->setDir('app', __DIR__ . '/config')
    ->setClass(\App\Config\Database::class);

Each one is documented in detail on the Loaders page:

Method Loads from
setArray() An associative array.
setFile() A PHP file that returns an array.
setDir() Every *.php file in a directory.
setClass() A class's / object's public properties.

Replacing the whole tree

set() writes a single key; replace() discards the whole tree and installs a new one:

$config->set('old', 'value');
$config->replace(['fresh' => 'data']);

$config->has('old');   // false
$config->get('fresh'); // 'data'

Object-style access

Top-level entries can be read as nested, read-only stdClass objects:

$config->set('db.host', 'localhost')->set('db.user', 'root');

$config->db->host; // 'localhost'
$config->db->user; // 'root'
$config->name;     // a scalar entry is returned as-is
$config->unknown;  // null for an unknown key

This is covered on the Object Access page.

Lifecycle

$config->version(); // the package version, e.g. '2.0.0'
$config->close();   // clear the tree (the instance stays usable)

close() empties the configuration but keeps the instance ready for reuse — the dotted-path and case-insensitive behaviour is preserved:

$config->set('a.b', 'c');
$config->close();

$config->all();           // []
$config->set('x.y', 'z'); // still works
$config->get('x.y');      // 'z'

The object's destructor calls close() automatically, so there is normally nothing to clean up by hand.

Dependency injection

Because Library is a plain object, it slots into any container. Prefer type-hinting against the interface so consumers can be tested with a stub:

use InitPHP\Config\Interfaces\ConfigInterface;
use InitPHP\Config\Library;

$container->set(ConfigInterface::class, function () {
    $config = new Library();
    $config->setDir('app', __DIR__ . '/config');

    return $config;
});

final class ReportService
{
    public function __construct(private ConfigInterface $config) {}

    public function timezone(): string
    {
        return $this->config->get('app.timezone', 'UTC');
    }
}

Heads-up: ConfigInterface covers get/set/has/remove/all. If a consumer needs the loaders (setFile, setDir, …) or object access, type-hint against Library instead.

Related reading

Clone this wiki locally