-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
A five-minute tour of the three entry points. Every snippet runs as-is.
Declare configuration as public properties and read it back through the shared API:
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('db.host'); // 'localhost' (nested array → dotted path)
$config->get('details', 'Not Found'); // 'Not Found' (default — key absent)
$config->has('name'); // trueSee Configuration Classes for the full story.
Library is an injectable object you fill at runtime. All write methods
return $this, so they chain:
use InitPHP\Config\Library;
$config = new Library();
$config->set('site.url', 'http://lvh.me')
->set('site.db.host', 'localhost')
->set('site.db.user', 'root');
$config->get('site.url'); // 'http://lvh.me'
$config->get('site.db.host'); // 'localhost'
$config->get('SITE.DB.HOST'); // 'localhost' (case-insensitive)
$config->all(); // ['site' => ['url' => ..., 'db' => [...]]]You can also seed it at construction:
$config = new Library([
'app' => ['name' => 'demo'],
'db' => ['host' => 'localhost'],
]);See The Library Object.
Config forwards every static call to a single, process-wide Library
instance — register configuration once, read it anywhere:
use InitPHP\Config\Config;
Config::setArray('site', ['url' => 'http://lvh.me']);
Config::get('site.url'); // 'http://lvh.me'
Config::has('site.url'); // true
Config::reset(); // discard the shared instance (useful in tests)See The Config Facade.
The Library object (and therefore the Config facade) can import
configuration from several sources:
// A PHP file that returns an array
Config::setFile('db', __DIR__ . '/config/db.php');
// Every *.php file in a directory, under a common prefix
Config::setDir('app', __DIR__ . '/config');
// A class's public properties
Config::setClass(\App\Config\Database::class);
Config::get('db.host');
Config::get('app.cache.driver');
Config::get('database.host');See Loaders for each method in depth.
A Library exposes top-level entries as nested, read-only objects:
$config = new Library();
$config->set('db.host', 'localhost')->set('db.user', 'root');
$config->db->host; // 'localhost'
$config->db->user; // 'root'See Object Access.
- Keys & Dotted Paths — how keys are parsed and normalised.
-
Loaders —
setArray,setFile,setDir,setClass. - API Reference — the exhaustive method list.
- Exceptions — what can go wrong, and how to handle it.
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