Skip to content

NullSessionBag

A SessionBagInterface that stores nothing.

What an application gets when it configures no session factory slot. The User hierarchy, CSRF token storage and OIDC state all read and write through the bag unconditionally, so they need something to talk to even where a session makes no sense — a console command, a queue worker, a stateless JSON API, a test context. This is that something, and it keeps “a User but * no sessions” expressible without a session backend, the way the old NullStorage did for the storage slot it replaces.

Writes are discarded rather than rejected: a component that opportunistically records something in the session should not have to know whether one exists. exists() answers false, so callers persisting default or empty state skip their write entirely instead of relying on that.

final class NullSessionBag implements SessionBagInterface

ImplementsSessionBagInterface
Since3.0.0
SourceSession/NullSessionBag.php
MethodDescription
destroy(): voidNo-op; there is no session state to discard.
exists(): boolAlways false, so callers persisting default or empty state skip their write entirely.
get(string $key, mixed $default = null): mixedAlways returns $default; nothing is ever stored.
getId(): stringAlways the empty string, the contract’s “no session” id.
has(string $key): boolAlways false; no key is ever present.
regenerate(bool $deleteOld = true, bool $privilegeTransition = false): voidNo-op; with no id and no contents there is nothing to rotate.
remove(string $key): voidNo-op; there is nothing stored to remove.
set(string $key, mixed $value): voidDiscards the value silently, so opportunistic writes need no session check.

public function destroy(): void

No-op; there is no session state to discard.

public function exists(): bool

Always false, so callers persisting default or empty state skip their write entirely.

Returns bool

public function get(string $key, mixed $default = null): mixed

Always returns $default; nothing is ever stored.

ParameterTypeDescription
$keystring
$defaultmixed

Returns mixed

public function getId(): string

Always the empty string, the contract’s “no session” id.

Returns string

public function has(string $key): bool

Always false; no key is ever present.

ParameterTypeDescription
$keystring

Returns bool

public function regenerate(bool $deleteOld = true, bool $privilegeTransition = false): void

No-op; with no id and no contents there is nothing to rotate.

ParameterTypeDescription
$deleteOldbool
$privilegeTransitionbool

public function remove(string $key): void

No-op; there is nothing stored to remove.

ParameterTypeDescription
$keystring

public function set(string $key, mixed $value): void

Discards the value silently, so opportunistic writes need no session check.

ParameterTypeDescription
$keystring
$valuemixed