Skip to content
A PHP Protocol Buffer Generator Plugin for protoc
C++ PHP Protocol Buffer
Find file
Latest commit 52a8a63 @bramp Work to make the full range of PHP ints decode as accurately as possi…
…ble, including those tricky negative numbers.
Failed to load latest commit information.
reference_app Random changes to the reference app for debuggin.
tests Work to make the full range of PHP ints decode as accurately as possi…
.gitignore Updated the gitignore to ignore more automake files.
LICENCE Added a LICENCE file.
Makefile.am Minor code style fixes and added more comments.
README.md Multiple changes to make the varint return ints when possible, and ot…
addressbook2.proto Updated the two proto files to be consistent.
addressbook3.proto Updated the two proto files to be consistent.
autogen.sh Updated the project to use autoconf/automake and made minor changes t…
configure.ac Added simple reference application to help with debugging.
php_enum.cc Refactored the large cc file into many smaller ones.
php_field.cc Refactored the large cc file into many smaller ones.
php_generator.cc Minor tweaks.
php_generator.h Added more type hinting, based on the version of PHP the code is gene…
php_message.cc PHP style use null instead of NULL.
php_method.cc Added more type hinting, based on the version of PHP the code is gene…
php_options.proto Refactored the large cc file into many smaller ones.
php_read.cc Minor code style fixes and added more comments.
php_service.cc Refactored the large cc file into many smaller ones.
php_size.cc Refactored the large cc file into many smaller ones.
php_tostring.cc Refactored the large cc file into many smaller ones.
php_util.cc Make PHPEscape not output ASCII characters above 0x7F. Even though th…
php_util.h A few changes to ensure the namespace stay scoped.
php_write.cc Refactored the large cc file into many smaller ones.
protoc-gen-php.cc Completed a few of the TODOs.
protocolbuffers.inc.php Work to make the full range of PHP ints decode as accurately as possi…
strutil.cc Updated the project to use autoconf/automake and made minor changes t…
strutil.h Updated the project to use autoconf/automake and made minor changes t…

README.md

PHP Protocol Buffer Generator

This is a plugin for Google's Protocol Buffer Generator, protoc. It generates PHP code from a .proto file. by Andrew Brampton (bramp.net) (c) 2010,2013

Supports:

  • Proto2 and Proto3
  • All features (Messages, Enums, Oneof, Maps)
  • Designed to be quick
  • Passes all compliance tests

Future:

  • JSON encoding
  • Support optimize_for=CODE_SIZE

Use

Once compiled and installed you can use it via protoc like so:

protoc --php_out=. your.proto

This should generate the file "your.proto.php", which should be able to encode and decode protocol buffer messages. When using the generated PHP code you must include the "protocolbuffers.inc.php" file.

Notes on Numbers

PHP uses signed integers, and depending on the platform may be 32 bit or 64 bit in size. Additionally PHP is typically compiled with IEEE 754 double precision floating point numbers, but may be compiled with single precision. This causes multiple problems for PHP using large numbers. As such the best appropimation is used when an exact representation is not available. On 32 bit platforms, signed integers are correct between -2^31 and 2^31-1, outside of that range floats are used. Double precision floats maintain integer accurancy up to 2^53, beyond that integers may not be precisce.

| Proto type | PHP type (32 bit) | PHP type (64 bit) | -------------------------------------------------------------------------------------- | bool | bool | bool | float / double | float | float | int32 | int | int | int64 / uint32 | int (between -2^31 and 2^31-1) otherwise float | int | uint64 | int (between 0 and 2^31-1) otherwise float | int (between 0 and 2^63-1) otherwise float | byte / string | string | string | enum | int | int

Install

This has only been tested on Linux and Mac. You will need the following libraries:

# For Debian based Linux
sudo apt-get install libprotobuf-dev libprotobuf-lite7 libprotobuf7 libprotoc-dev libprotoc7

# For Mac
brew install protobuf

To build:

./autogen.sh
./configure
make
sudo make install

Licence (Simplified BSD License)

Copyright (c) 2010-2013, Andrew Brampton All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Something went wrong with that request. Please try again.