-
Notifications
You must be signed in to change notification settings - Fork 0
Basic Usage
This page covers the every-day, flat-mode workflow: a single namespace of string-keyed values. Dotted-path semantics are documented separately under Nested Data.
use InitPHP\ParameterBag\ParameterBag;
$bag = new ParameterBag(); // empty
$bag = new ParameterBag(['locale' => 'en_US']); // pre-seeded
$bag = new ParameterBag($_GET); // from a superglobalThe constructor's second argument is the options array — see Configuration. Without options the bag starts in flat mode and is case-sensitive.
$bag = new ParameterBag(['user' => 'alice', 'role' => null]);
$bag->get('user'); // 'alice'
$bag->get('missing'); // null
$bag->get('missing', 'fallback'); // 'fallback'
$bag->get('role', 'fallback'); // nullThe last line is important: get() returns the stored value
whenever the key is present, even when that value is null. The
$default only fires when the key is genuinely absent.
has() is the authoritative existence check. A stored null
counts as present:
$bag->has('user'); // true
$bag->has('role'); // true (null is present)
$bag->has('missing'); // falseIn v1,
has()andget()shared a magic-string sentinel, so a storednulllooked identical to a missing key. v2 usesarray_key_existsinternally —nullis now a first-class value.
$bag = new ParameterBag();
$bag->set('host', 'localhost')
->set('port', 5432)
->set('flags', ['retry' => true]);Three things to remember:
-
set()returns$this, so writes chain. - Existing keys are overwritten without notice.
- Array values are normalised through the same code path the
constructor uses (recursive lower-casing when
caseInsensitiveis on; otherwise unchanged).
$bag = new ParameterBag(['a' => 1, 'b' => 2, 'c' => 3]);
$bag->remove('a'); // removes 'a'
$bag->remove('b', 'c'); // removes both (variadic)
$bag->remove('does-not-exist'); // silent no-opremove() returns $this and ignores missing keys.
$bag = new ParameterBag(['a' => 1, 'b' => 2]);
$bag->count(); // 2 (also: count($bag), see Countable)
$bag->keys(); // ['a', 'b']
$bag->values(); // [1, 2]
$bag->all(); // ['a' => 1, 'b' => 2]
$bag->isEmpty(); // falsekeys(), values(), and all() reflect top-level entries only.
Nested arrays are returned as-is — they are not unwound.
$bag = new ParameterBag(['a' => 1, 'b' => 2]);
$bag->replace(['only' => 'value']);
$bag->all();
// ['only' => 'value']replace() swaps the entire stack but preserves the active
options (isMulti, separator, caseInsensitive). The new
payload is run through the same normalisation as the constructor.
| Method | Stack | Options |
|---|---|---|
clear() |
emptied | preserved |
close() |
emptied | reset to defaults (isMulti=false, separator='.', caseInsensitive=false) |
$bag = new ParameterBag(
['a' => 1],
['isMulti' => true, 'separator' => '|', 'caseInsensitive' => true]
);
$bag->clear();
// stack: []
// isMulti=true, separator='|', caseInsensitive=true
$bag->close();
// stack: []
// isMulti=false, separator='.', caseInsensitive=falseReach for clear() when reusing a long-lived bag for a new payload
under the same configuration. Reach for close() when handing a
recycled bag to unrelated code that should not inherit your
options.
-
$bag->get('foo.bar')in flat mode: the literal stringfoo.baris the key. The dot has no special meaning unlessisMultiis on. See Nested Data. -
Forgetting that v2 is case-sensitive:
set('User', 'a')thenget('user')returnsnull. See Case Sensitivity. -
$bag[] = $value: ParameterBag is a string-keyed structure; the append form raisesParameterBagInvalidArgumentException. See Exceptions.
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