Using Google Protcol Buffers with PHP and protoc-gen-php
Protocol Buffers are a binary data transfer protocol from Google. You define the structure of the data using a C-like text file, then compile that protocol buffer file into a library appropriate for the language you are using. Google officially supports Java, Python and C++.
PHP Protocol Buffers
UPDATE!
I haven’t tested it yet, but DRSlump commented below to let me know about a new, alive and mostly complete implementation he is using and working on. I haven’t tried it yet since it uses PHP 5.3 and we haven’t quite moved over from 5.2 yet, but here it is for your enjoyment:
I could find no complete implementation for PHP at this time. There are several 1/2 completed protocol buffer implementations out there for PHP, most of which date back to near the initial protocol buffer announcement from Google.
If you are going to use Protocol Buffers in a PHP project, you will probably want to evaluate the features and approach of each of these projects. For the project I am working on I decided to go with protoc-gen-php since it seemed to have the most complete (3/4ths complete?!) implementation and the most recent update.
Status of protoc-gen-php
There were two branches of protoc-gen-php when I started using it. Bramp was the original author and responded to emails, Iamamused has a few more features, but didn’t respond to any of my emails. I took the email response to mean, in part, that despite the lack of updates that Bramp was still interested in at least maintaining the project.After forking his project to add some features I needed, he did in fact indicate that he will indeed merge appropriate changes from my fork and from Iamamused back into the main branch.
Until that happens though you’ve got three choices:
- Bramp’s branch — the original and probably best long-term choice
- Iamamused’s branch — more features than Bramp’s branch (sint, for example) but no indication if he will be maintaining it long term
- Stuporglue’s branch (mine) — See below for features and caveats
My fork of Protoc-gen-php
I don’t really have interest in being a project maintainer long term. If you use my branch, I’m happy to help in whatever capacity I’m able until Bramp merges the features I’ve added back into his branch.
Here’s what I’ve added:
- Signed Int support (sint32 / sint64)
- Float and Double support
- Support for the [packed=false] option so that repeating packables can be used (ie. not packed support, just the ability to specify non-packed)
The big caveat with float support is that PHP uses doubles to represent both floats and doubles, so when a float-double gets packed into actual an actual float in the protocol format it does lose precision. The best solution will be to either use a string or a double.
Need Other Features Added?
I have implemented the features I did because I needed them for the project I am working on. I’d be happy to add other features on a consulting basis, or to merge in your changes if you submit patches to me. Like I said though, hopefully it will be merged back into Bramp’s branch pretty soon, and he can handle the patches. It would be one less thing for me to worry about!
