Skip to content

SessionCodecInterface

Serializes a session payload for storage, and reads it back.

A session’s stored form is a wire format: whatever writes it has to agree with whatever reads it, including a different backend reading a payload another one wrote, and a build with different extensions available. That agreement is this interface’s whole purpose — a SessionPersistenceInterface implementation decides where a payload goes and delegates what it looks like here.

Implement this to change the stored form — encryption at rest, a compressed envelope, a format an external consumer already reads — and hand it to the persistence backend.

interface SessionCodecInterface

Implemented bySessionCodec
Since3.2.0
SourceSession/SessionCodecInterface.php
MethodDescription
[`decode(string $payload): array<string, mixed>null`](#decode)
encode(array<string, mixed> $data): stringEncode session data for storage.

abstract public function decode(string $payload): array<string, mixed>|null

Decode a stored payload, or null when it does not hold readable session data.

Null rather than an exception for unreadable input: a payload written by an older format, a truncated row, or a value that decodes to something that is not a session are all reasons to treat the session as absent and start a new one, not to fail the request.

ParameterTypeDescription
$payloadstring

Returns array``<``string``, ``mixed``>``|``null

abstract public function encode(array<string, mixed> $data): string

Encode session data for storage.

ParameterTypeDescription
$dataarray``<``string``, ``mixed``>

Returns string

ThrowsWhen
StorageExceptionIf the data cannot be encoded at all.