Skip to main content
Search Documentation & Spec
Type to search all 17 pages

OmniBuf
for PHP.

Pure PHP 8.3+ library on Packagist with a bundled Composer plugin binary and an optional Rust-backed ext-omnibuf extension. Annotate plain PHP classes with attributes, marshal existing google/protobuf messages, or generate typed classes and lazy Opaque views.

Install via Composer
$ composer require omnibuf/omnibuf
Optional: Prebuilt ext-omnibuf
$ curl -fsSL https://raw.githubusercontent.com/omnibuf/omnibuf-php/main/scripts/install-ext.sh | sh

Three ways to use OmniBuf in PHP.

All three emit identical v1 DRAFT-1 wire bytes on 64-bit PHP 8.3 and newer.

PHP 8 attribute-annotated classes

Tag class properties with #[OmniBufMessage] and #[OmniBufField]. Metadata is cached per class on first use so repeated marshals skip reflection overhead.

#[OmniBufField] Class
<?php
use OmniBuf\Attributes\OmniBufField;
use OmniBuf\Attributes\OmniBufMessage;
use OmniBuf\OmniBuf;

#[OmniBufMessage('Location')]
final class Location
{
    #[OmniBufField(3, 'string')]
    public string $city = '';

    #[OmniBufField(4, 'uint32')]
    public int $rack = 0;
}

$loc = new Location();
$loc->city = 'FRA';
$loc->rack = 42;

$bytes = OmniBuf::marshal($loc, deterministic: true);
$decoded = OmniBuf::unmarshal($bytes, Location::class);
PHP property type defaults
PHP type
Protobuf type
int
sint64
bool
bool
float
double
string (or untyped)
string
Class
message
array
repeated / map

Untyped properties default to string, int defaults to signed sint64, and float defaults to double. Pass a type string to #[OmniBufField] (such as 'uint32', 'uint64', 'fixed32', 'fixed64', 'float32', or 'bytes') to override it. PHP arrays carry no element types at runtime, so repeated fields need elementWire: and maps need wire: 'map' with keyWire: and valueWire:.

Existing google/protobuf messages

OmniBuf::marshal and OmniBuf::unmarshal accept standard Google\Protobuf\Internal\Message objects directly via DescriptorPool with no extra code generator required.

google/protobuf Bridge
<?php
use OmniBuf\OmniBuf;
use Omnibuf\Bench\PeerUpdate;

$bytes = OmniBuf::marshal($protoPeerUpdate, deterministic: true);
$decoded = OmniBuf::unmarshal($bytes, PeerUpdate::class);

Generated classes with protoc-gen-omnibuf-php

vendor/bin/protoc-gen-omnibuf-php emits typed PHP 8.3+ message classes and lazy Opaque* views that skip untouched fields in O(fields) time and splice modified bytes directly.

buf.gen.yaml
version: v2
clean: true
plugins:
  - local: vendor/bin/protoc-gen-omnibuf-php
    out: gen
Lint & Generate with Buf
$ buf lint
$ buf generate
Lazy Opaque View & Wire Splice
<?php
use OmniBuf\Generated\Omnibuf\Bench\OpaquePeerUpdate;

$view = new OpaquePeerUpdate($packetBytes);
if ($view->opaqueLocation()?->getCity() === 'Frankfurt') {
    $view->setSequence($view->getSequence() + 1);
}
$updatedPacket = $view->marshal();

gRPC in PHP with Grpc\BaseStub.

OmniBuf\Grpc\GrpcCodec provides serializer, deserializer, and content-type metadata helpers for grpc/grpc stubs with zero runtime dependency on grpc/grpc.

Codec names: "omnibuf" (compact) · "x-omnibuf" (with Field #0)

OmniBuf\Grpc\GrpcCodec
use OmniBuf\Grpc\GrpcCodec;

// Inside a Grpc\BaseStub subclass (zero runtime dependency on grpc/grpc)
$call = $this->_simpleRequest(
    '/acme.v1.Svc/Ping',
    $request,
    GrpcCodec::deserialize(PingResponse::class),
    GrpcCodec::callMetadata(),
    ['serializer' => static fn(object $m): string => GrpcCodec::serialize($m)],
);

PHP questions.

Does omnibuf/omnibuf require a PECL or C extension?

No. Pure PHP 8.3+ (64-bit) works out of the box anywhere Composer runs, encoding around 19x faster than pure-PHP google/protobuf. An optional pure-Rust ext-omnibuf extension (ext-php-rs) is included in ext/rust and detected automatically when loaded, accelerating encode, decode, opaque views, splicing, and blind decode.

How is protoc-gen-omnibuf-php installed?

Composer links vendor/bin/protoc-gen-omnibuf-php automatically on composer require omnibuf/omnibuf. Point buf.gen.yaml at that binary to generate typed classes and lazy Opaque* views.

How does OmniBuf::decodeBlind inspect schema-less packets?

OmniBuf::decodeBlind($bytes) and OmniBuf::decodeBlindJson($bytes) decode any OmniBuf packet straight from its wire types. Keys default to field_1, field_2, ... or resolve to Field #0 names when selfDescribing: true was passed on marshal.

Example
<?php
use OmniBuf\OmniBuf;

$bytes = OmniBuf::marshal($loc, selfDescribing: true);
$json = OmniBuf::decodeBlindJson($bytes);

Language implementations.

v1 DRAFT-1 · GitHub Feedback →
Architecture, Tooling & Wire Specification