-
Notifications
You must be signed in to change notification settings - Fork 0
Object Access
A Library exposes top-level configuration entries
as read-only nested objects through PHP's magic property access. It
is a convenience for reading deep config without dotted strings.
Object access is a feature of
Libraryonly. TheClassesbase does not provide it, and theConfigfacade is static — there is no instance to read properties from.
use InitPHP\Config\Library;
$config = new Library();
$config->set('db.host', 'localhost')
->set('db.user', 'root');
$db = $config->db; // stdClass
$db->host; // 'localhost'
$db->user; // 'root'Nesting works to any depth:
$config->set('services.cache.driver', 'redis');
$config->services->cache->driver; // 'redis'- A scalar top-level entry is returned as-is.
- An unknown key returns
null.
$config->set('name', 'InitPHP');
$config->name; // 'InitPHP' (scalar, returned directly)
$config->unknown; // nullLibrary::__get($name) looks the key up in the underlying store. If the
value is an array it is converted, recursively, into a nested
stdClass; scalars pass through unchanged; an absent key yields null.
// Equivalent ways to read the same value:
$config->get('db.host'); // via the dotted-path API
$config->db->host; // via object accessBecause the store is case-insensitive, the object's property names are the lower-cased form of the keys:
$config->set('DB.Host', 'localhost');
$config->db->host; // 'localhost'
// $config->DB->Host would not match — keys are normalised to lower-case.__get builds a fresh object from the current data each time it is
called. Mutating the returned object does not write back into the
configuration:
$config->set('db.host', 'localhost');
$db = $config->db;
$db->host = 'changed'; // touches the local copy only
$config->get('db.host'); // 'localhost' (unchanged)To change configuration, use set().
- The Library Object — the full object API.
- Keys & Dotted Paths — the dotted-string equivalent.
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