All classes live in the Resp3 namespace. The extension also defines one global function.
resp3_version()
resp3_version(): string
Returns the version of the php-resp3 extension, for example 0.3.0.
Resp3\Parser
final class Parser{ public function __construct( int $maxDepth = 100, int $maxBulk = 536870912, int $maxAggregateCount = 1000000, bool $queuePushes = false, ) {}}
An incremental RESP3 parser. The class is final and not serializable.
Constructor arguments
| Argument | Default | Meaning |
|---|---|---|
int $maxDepth |
100 |
Aggregate nesting limit. Maximum 10000. |
int $maxBulk |
536870912 (512 MiB) |
Maximum bytes per bulk string. Maximum 2 GiB. |
int $maxAggregateCount |
1000000 |
Maximum elements per array, set or push, or pairs times 2 for a map. Maximum 100000000. Also bounds the push queue. |
bool $queuePushes |
false |
When true, top-level push frames (>) are queued and read with hasPush() and nextPush() instead of being returned by next(). |
Calling __construct() a second time on an existing parser discards its state. Use reset() to recycle an instance.
Methods
| Method | Purpose |
|---|---|
feed(string $bytes): void |
Append bytes to the internal buffer. No parse work. Never throws. |
hasNext(): bool |
Drive the state machine. true when a complete message is buffered and ready for next(). Throws on protocol error. |
next(): mixed |
Return the message detected by hasNext(). |
hasPush(): bool |
true when a push message is queued. Always false in default mode. |
nextPush(): ?PushMessage |
Remove and return the oldest queued push, or null when the queue is empty. |
lastAttributes(): ?array |
Attributes of the most recently returned value, read once. |
reset(): void |
Discard all internal state and clear an error latch. Never throws. |
hasNext() and next()
hasNext() advances the state machine and holds the parsed value until next() consumes it. If hasNext() has not been called (or returned false) since the last next(), next() advances the state machine itself. next() throws LogicException when no complete message is available.
Server errors (- and !) come out of next() as Resp3\RedisException instances, not thrown, so consumers can route on instanceof. Nested errors and verbatim strings (=) are wrapped at any depth, so an error inside an EXEC result is an exception object in the result array.
Length prefixes
Only $-1 and *-1 are null lengths (RESP2 compatibility). ~-1, >-1, %-1, |-1, !-1, =-1, -0 and lengths with leading zeros are protocol errors.
Error latch
After a protocol error, hasNext() and next() throw parser is in error state; call reset() until you call reset(). hasPush() and nextPush() first hand out pushes queued before the fault, then throw once the queue is empty.
hasPush() and nextPush()
Both drive the state machine like hasNext(), so a buffered push is found without a regular reply. They do not parse past an unconsumed regular reply: a push behind a pending reply becomes visible only after that reply is taken with next().
The queue holds at most maxAggregateCount pushes. When it is full, the next push is a protocol error (push queue limit exceeded; drain with nextPush()) and the parser must be reset. Drain with hasPush() and nextPush() after each reply. Nested > frames are never queued; they stay plain arrays inside their parent.
In default mode pushes are returned from next() as Resp3\PushMessage.
lastAttributes()
Reading returns the attribute payload from the most recent | frame and clears the slot, so a second call returns null until the parser receives a new attribute frame. Attributes belong to the message that follows them and are also cleared when the next top-level message starts.
Resp3\RedisException
class RedisException extends \RuntimeException{ public string $prefix = '';}
$prefix holds the leading uppercase token of the server error, for example ERR, WRONGTYPE, MOVED, ASK or NOAUTH. It is an empty string when the message has no such token. Parser faults (malformed wire bytes) use PROTOCOL. Route on $prefix instead of string matching the message.
Resp3\VerbatimString
final class VerbatimString{ public readonly string $type; public readonly string $value; public function __construct(string $type, string $value) {}}
Wraps a RESP3 verbatim string (=). $type carries the format prefix (for example txt or mkd) separately from the payload.
$type is server-supplied. The parser only accepts a 3-character ASCII alphanumeric prefix; anything else falls back to an empty $type with the full payload in $value. Treat $type as untrusted when interpolating it into log lines, headers, filenames or shell commands.
Resp3\PushMessage
final class PushMessage{ public readonly array $payload; public function __construct(array $payload) {}}
Wraps a RESP3 push message (>), so consumers can tell server-pushed events (pubsub, client tracking invalidation) from regular replies with instanceof.
Type mapping
| RESP3 wire | PHP value |
|---|---|
+OK\r\n simple string |
string |
:42\r\n integer |
int |
$N\r\n...\r\n bulk string (binary-safe) |
string |
$-1\r\n null bulk |
null |
*N\r\n... array |
array (indexed) |
*-1\r\n null array |
null |
_\r\n null |
null |
,1.5\r\n, ,inf\r\n, ,nan\r\n |
float |
#t\r\n, #f\r\n boolean |
bool |
(N...\r\n big number |
string (PHP has no native bignum) |
%N\r\n... map |
array (associative) |
~N\r\n... set |
array (indexed; PHP has no native set) |
=N\r\nxxx:payload\r\n verbatim string |
Resp3\VerbatimString |
>N\r\n... push |
Resp3\PushMessage |
|N\r\n... attribute |
attached to the parser; read via lastAttributes() |
-ERR ...\r\n error |
Resp3\RedisException (returned, not thrown) |
!N\r\n...\r\n blob error |
Resp3\RedisException (returned, not thrown) |
Security limits
The parser treats wire input as untrusted. A malicious or buggy server can send arbitrary bytes; the goal is to throw a Resp3\RedisException rather than crash the PHP process or exhaust memory. The three constructor caps protect against adversarial input:
$p = new Resp3\Parser( maxDepth: 100, // aggregate nesting (default 100, max 10000) maxBulk: 536_870_912, // bytes per bulk string (default 512 MiB, max 2 GiB) maxAggregateCount: 1_000_000, // elements per array/set/push (or pairs for map));
The parser also caps inline lines (+, -, :, ,, #, (, _) at 64 KiB, rejects length values with more than 19 digits, detects signed integer overflow before it happens, and refuses to multiply a map or attribute count when doubling it would wrap. All of these surface as RESP3 parse error: ... exceptions you can catch.
Unsupported
- Streamed types (
$?,*?,~?,%?). The parser rejects the?length marker as a non-digit in the length. - Inline commands (
PING\r\nstyle input). The parser rejects an unknown first byte with a message that points at the direction mismatch.