-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
A five-minute tour of the public API. Every snippet runs as-is.
use InitPHP\ParameterBag\ParameterBag;
$bag = new ParameterBag([
'app_name' => 'demo',
'debug' => true,
]);
$bag->get('app_name'); // 'demo'
$bag->get('missing'); // null
$bag->get('missing', 'fallback'); // 'fallback'
$bag->set('app_name', 'production')
->set('locale', 'en_US')
->remove('debug');
$bag->all();
// ['app_name' => 'production', 'locale' => 'en_US']set() / remove() / merge() / replace() all return $this so
writes chain naturally.
If the constructor payload contains nested arrays, the bag switches
into multi mode and treats the configured separator (. by
default) as a path delimiter:
$config = new ParameterBag([
'database' => [
'dsn' => 'mysql:host=localhost',
'username' => 'root',
'password' => 'secret',
],
]);
$config->get('database.username'); // 'root'
$config->has('database.charset'); // false
$config->get('database.charset', 'utf8mb4'); // 'utf8mb4'
$config->set('database.charset', 'utf8mb4');
$config->remove('database.password');To force multi mode on an initially empty bag, pass it explicitly:
$bag = new ParameterBag([], ['isMulti' => true]);
$bag->set('cache.driver', 'redis');$bag = new ParameterBag(
['db' => ['user' => 'root']],
['separator' => '|']
);
$bag->get('db|user'); // 'root'By default keys are case-sensitive (User ≠ user). Opt back into
the legacy v1 behaviour with caseInsensitive => true:
$bag = new ParameterBag(
['User' => 'alice'],
['caseInsensitive' => true]
);
$bag->get('USER'); // 'alice'The bag implements ArrayAccess, Countable, and IteratorAggregate,
so it works wherever PHP's standard collection contracts are
recognised:
$bag = new ParameterBag(['a' => 1, 'b' => 2]);
count($bag); // 2
$bag['c'] = 3; // set via ArrayAccess
isset($bag['c']); // true
unset($bag['c']);
foreach ($bag as $key => $value) {
echo "$key=$value\n";
}In multi mode the ArrayAccess offset can be a dotted path:
$bag = new ParameterBag(['db' => ['user' => 'root']]);
$bag['db.user']; // 'root'-
Basic Usage — the every-day
get/set/has/removeAPI. - Nested Data — multi mode in depth.
- Configuration — every option, with examples.
- API Reference — exhaustive method list.
initphp/parameterbag · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Usage
Reference
Practical Guides
Migration & Help