PHPackages                             centraldesktop/protobuf-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. centraldesktop/protobuf-php

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

centraldesktop/protobuf-php
===========================

PHP implementation of Google's Protocol Buffers

1.0.1(10y ago)30616.6k—5.1%25[1 issues](https://github.com/centraldesktop/Protobuf-PHP/issues)8MITPHPPHP &gt;=5.5.0

Since Jan 16Pushed 8y ago16 watchersCompare

[ Source](https://github.com/centraldesktop/Protobuf-PHP)[ Packagist](https://packagist.org/packages/centraldesktop/protobuf-php)[ Docs](https://github.com/drslump/Protobuf-PHP)[ RSS](/packages/centraldesktop-protobuf-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (36)Used By (8)

Protobuf for PHP
================

[](#protobuf-for-php)

Protobuf for PHP is an implementation of Google's Protocol Buffers for the PHP language, supporting its binary data serialization and including a `protoc`plugin to generate PHP classes from .proto files.

Great effort has been put into generating PHP files that include all sort of type hints to aide IDE's with autocompletion. Therefore, it can not only be used to communicate with Protocol Buffers services but also as a generation tool for *data objects* no matter what the final serialization is.

For more information see the [included man pages](http://drslump.github.com/Protobuf-PHP/).

Requirements
------------

[](#requirements)

- PHP 5.3
- Pear's Console\_CommandLine (for the protoc wrapper tool)
- Google's `protoc` compiler version 2.3 or above
- GMP or BC Math extensions ¹

¹ Only needed for negative values in `int32`, `int64` or `fixed64` types. See the *known issues* section.

Features
--------

[](#features)

### Working

[](#working)

- Standard types (numbers, string, enums, messages, etc)
- Extensions, Unknown and Packed fields
- Generate service interfaces
- Protoc compiler plugin to generate the PHP classes
- Template based code generation. Go crazy and customize the generated code :)
- Include comments from .proto files in the generated files
- Pluggable serialization backends (codecs)
    - Standard Binary
    - Standard TextFormat ¹
    - PhpArray
    - JSON
    - [ProtoJson](https://github.com/drslump/ProtoJson) (*TagMap* and *Indexed* variants)
    - XML
- Reflection capabilities
- Dynamic messages with annotations support (no code generation step required)
- Lazy decoding of messages to improve the performance in real world scenarios

¹ Only serialization is supported in this codec

### Future

[](#future)

- Speed optimized code generation mode

Example usage
-------------

[](#example-usage)

```
$person = new Tutorial\Person();
$person->name = 'DrSlump';
$person->setId(12);

$book = new Tutorial\AddressBook();
$book->addPerson($person);

// Use default codec
$data = $book->serialize();

// Use custom codec
$codec = new \DrSlump\Protobuf\Codec\Json();
$data = $codec->encode($book);
// ... or ...
$data = $book->serialize($codec);
```

Installation
------------

[](#installation)

```
composer install

```

Known issues
------------

[](#known-issues)

### Types

[](#types)

PHP is very weak when dealing with numbers processing. Several work arounds have been applied to the standard binary codec to reduce incompatibilities between Protobuf types and PHP ones.

- Protobuf stores floating point values using the [IEEE 754](http://en.wikipedia.org/wiki/IEEE_754) standard with 64bit words for the `double` and 32bit for the `float` types. PHP supports IEEE 754 natively although the precission is platform dependant, however it typically supports 64bit doubles. It means that if your PHP was compiled with 64bit sized doubles (or greater) you shouldn't have any problem encoding and decoded float and double typed values with Protobuf.
- Integer values are also [platform dependant in PHP](http://www.php.net/manual/en/language.types.integer.php). The library has been developed and tested against PHP binaries compiled with 64bit integers. The encoding and decoding algorithm should in theory work no matter if PHP uses 32bit or 64bit integers internally, just take into account that with 32bit integers the numbers cannot exceed in any case the `PHP_INT_MAX` value (2147483647).

    While Protobuf supports unsigned integers PHP does not. In fact, numbers above the compiled PHP maximum integer (`PHP_INT_MAX`, 0x7FFFFFFFFFFFFFFF for 64bits) will be automatically casted to doubles, which typically will offer 53bits of decimal precission, allowing to safely work with numbers upto 0x20000000000000 (2^53), even if they are represented in PHP as floats instead of integers. Higher numbers will loose precission or might even return an *infinity* value, note that the library does not include any checking for these numbers and using them might provoke unexpected behaviour.

    Negative values when encoded as `int32`, `int64` or `fixed64` types require the big integer extensions [GMP](http://www.php.net/gmp) or [BC Math](http://www.php.net/bc) (the later only for 64bit architectures) to be available in your PHP environment. The reason is that when encoding these negative numbers without using *zigzag* the binary representation uses the most significant bit for the sign, thus the numbers become above the maximum supported values in PHP. The library will check for these conditions and will automatically try to use GMP or BC to process the value.

### Strings

[](#strings)

The binary codec expects strings to be encoded using UTF-8. PHP does not natively support string encodings, PHP's string data type is basically a length delimited stream of bytes, so it's not trivial to include automatic encoding conversion into the library encoding and decoding routines. Instead of trying to guess or offer a configuration interface for the encoding, the binary codec will process the `string` type just as it would process `byte` one, delegating on your application the task of encoding or decoding in the desired character set.

### Memory usage

[](#memory-usage)

Large messages might be troublesome since the way the library is modelled does not allow to parse or serialize messages as streams, instead the whole operation is performed in memory, which allows for faster processing but could consume too much RAM if messages are too large.

### Unknown fields

[](#unknown-fields)

Since wire types are different across different codec's formats, it's not possible to transcode unkwnon fields consumed in one codec to another. This means, for example, that when consuming a message using the binary codec, if it contains unknown fields they won't be included when serializing the message using the Json codec.

Generating PHP classes
----------------------

[](#generating-php-classes)

The generation tool is designed to be run as a `protoc` plugin, thus it should work with any proto file supported by the official compiler.

```
protoc --plugin=protoc-gen-php --php_out=./build tutorial.proto

```

To make use of non-standard options in your proto files (like `php.namespace`) you'll have to import the `php.proto` file included with the library. It's location will depend on where you've installed this library.

```
protoc -I=./Protobuf-PHP/library/DrSlump/Protobuf/Compiler/protos \
       --plugin=protoc-gen-php --php_out=./build tutorial.proto

```

In order to make your life easier, the supplied protoc plugin offers an additional execution mode, where it acts as a wrapper for the `protoc` invocation. It will automatically include the `php.proto` path so you don't need to worry about it.

```
protoc-gen-php -o ./build tutorial.proto

```

Testing
-------

[](#testing)

mkdir -p test/generated ./protoc-gen-php.php -o test/generated -Dmultifile=true -i ./test/library/DrSlump/Protobuf/Test/protos/ ./test/library/DrSlump/Protobuf/Test/protos/\*.proto ./vendor/bin/phpunit

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community33

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 69.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~61 days

Recently: every ~155 days

Total

25

Last Release

3037d ago

Major Versions

0.7.3 → 1.0.02016-04-15

PHP version history (2 changes)0.5.0PHP &gt;=5.3.0

0.6.6PHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/f8c5fe7851c2f08861357cef4c0f1144beecddc53b50bbb8e8d73de85f087762?d=identicon)[treyhyde](/maintainers/treyhyde)

---

Top Contributors

[![drslump](https://avatars.githubusercontent.com/u/110784?v=4)](https://github.com/drslump "drslump (69 commits)")[![treyhyde](https://avatars.githubusercontent.com/u/62077?v=4)](https://github.com/treyhyde "treyhyde (15 commits)")[![hhilbert](https://avatars.githubusercontent.com/u/1935262?v=4)](https://github.com/hhilbert "hhilbert (3 commits)")[![slimus](https://avatars.githubusercontent.com/u/478249?v=4)](https://github.com/slimus "slimus (3 commits)")[![bdferris](https://avatars.githubusercontent.com/u/630674?v=4)](https://github.com/bdferris "bdferris (2 commits)")[![shumkov](https://avatars.githubusercontent.com/u/24296?v=4)](https://github.com/shumkov "shumkov (1 commits)")[![alanpoulain](https://avatars.githubusercontent.com/u/10920253?v=4)](https://github.com/alanpoulain "alanpoulain (1 commits)")[![webmaster777](https://avatars.githubusercontent.com/u/2650744?v=4)](https://github.com/webmaster777 "webmaster777 (1 commits)")[![clouiepgi](https://avatars.githubusercontent.com/u/13024119?v=4)](https://github.com/clouiepgi "clouiepgi (1 commits)")[![cookpan001](https://avatars.githubusercontent.com/u/2258980?v=4)](https://github.com/cookpan001 "cookpan001 (1 commits)")[![kdiedrickpgi](https://avatars.githubusercontent.com/u/51805168?v=4)](https://github.com/kdiedrickpgi "kdiedrickpgi (1 commits)")[![richardfullmer](https://avatars.githubusercontent.com/u/384602?v=4)](https://github.com/richardfullmer "richardfullmer (1 commits)")

---

Tags

protobufprotocol bufferserializing

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/centraldesktop-protobuf-php/health.svg)

```
[![Health](https://phpackages.com/badges/centraldesktop-protobuf-php/health.svg)](https://phpackages.com/packages/centraldesktop-protobuf-php)
```

###  Alternatives

[protobuf-php/protobuf

PHP implementation of Google's Protocol Buffers

2701.4M18](/packages/protobuf-php-protobuf)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[protobuf-php/protobuf-plugin

PHP Code generator plugin from Google's Protocol Buffers

57141.6k17](/packages/protobuf-php-protobuf-plugin)[stanley-cheung/protobuf-php

PHP implementation of Google's Protocol Buffers

23232.5k1](/packages/stanley-cheung-protobuf-php)[mck89/peast

Peast is PHP library that generates AST for JavaScript code

18934.7M29](/packages/mck89-peast)[ivopetkov/html5-dom-document-php

HTML5 DOMDocument PHP library (extends DOMDocument)

6031.4M49](/packages/ivopetkov-html5-dom-document-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
