-
Notifications
You must be signed in to change notification settings - Fork 0
Configuration Classes
InitPHP\Config\Classes is an abstract base class that turns a
subclass's properties into a configuration tree. It is the most natural
fit for configuration that ships with your application as code —
typed, discoverable, and version-controlled alongside the rest of your
source.
Extend Classes and declare your configuration as properties with
default values:
use InitPHP\Config\Classes;
final class AppConfig extends Classes
{
public string $url = 'http://lvh.me';
public string $name = 'LocalHost';
/** @var array<string, string> */
public array $db = [
'host' => 'localhost',
'user' => 'root',
];
}$config = new AppConfig();
$config->get('url'); // 'http://lvh.me'
$config->get('name'); // 'LocalHost'
$config->get('db.host'); // 'localhost' (nested array → dotted path)
$config->get('db.user'); // 'root'
$config->has('URL'); // true (case-insensitive)
$config->all(); // ['url' => ..., 'name' => ..., 'db' => [...]]A config class is not frozen. Because it shares the
ConfigInterface surface, you can
set and remove after construction:
$config->set('db.pass', 'secret');
$config->get('db.pass'); // 'secret'
$config->remove('db.pass');
$config->has('db.pass'); // false- The property default values declared on the class are imported.
- Both public and protected properties are included — they are
visible from the
Classesbase in the inheritance chain. A subclass's private properties are not imported. - The internal
parameterBaginfrastructure property is never part of the tree.
final class WithVisibility extends Classes
{
public string $publicValue = 'public';
protected string $protectedValue = 'protected';
private string $privateValue = 'private';
}
$config = new WithVisibility();
$config->get('publicValue'); // 'public'
$config->get('protectedValue'); // 'protected'
$config->has('privateValue'); // false
$config->has('parameterBag'); // falseNote: this differs from
Library::setClass(), which imports public properties only. The difference is intentional:Classesinspects itself from within the hierarchy, whereassetClass()inspects an unrelated class from the outside.
Classes::__construct() reads the declared property defaults of the
concrete subclass (get_class_vars(static::class)), drops the internal
infrastructure property, and seeds the underlying store in dotted-path,
case-insensitive mode. You do not call parent::__construct() with any
arguments — just extend and declare properties.
Use Classes when… |
Reach for Library when… |
|---|---|
| Config is known at author time. | Config is loaded from files/env at runtime. |
| You want IDE autocompletion and types. | Sources are dynamic (directories, env). |
| Config is small and stable. | You compose many sources into one tree. |
- Keys & Dotted Paths — how nested arrays become dotted keys.
- Typed Config Class recipe — a fuller example.
- API Reference — the class surface.
initphp/config · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Concepts
Loading Configuration
Reference
Practical Guides
Migration & Help