Home

Awesome

Protobuf for PHP

Build Status Coverage Status Total Downloads License

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.

Installation

Run the following composer commands:

$ composer require "protobuf-php/protobuf"

Overview

This tutorial provides a basic introduction to working with protocol buffers. By walking through creating a simple example application, it shows you how to

Why Use Protocol Buffers?

The example we're going to use is a very simple "address book" application that can read and write people's contact details to and from a file. Each person in the address book has a name, an ID, an email address, and a contact phone number.

How do you serialize and retrieve structured data like this? There are a few ways to solve this problem:

Protocol buffers are the flexible, efficient, automated solution to solve exactly this problem. With protocol buffers, you write a .proto description of the data structure you wish to store. From that, the protocol buffer compiler creates a class that implements automatic encoding and parsing of the protocol buffer data with an efficient binary format. The generated class provides getters and setters for the fields that make up a protocol buffer and takes care of the details of reading and writing the protocol buffer as a unit. Importantly, the protocol buffer format supports the idea of extending the format over time in such a way that the code can still read data encoded with the old format.

Defining Your Protocol Format

To create your address book application, you'll need to start with a .proto file. The definitions in a .proto file are simple: you add a message for each data structure you want to serialize, then specify a name and a type for each field in the message. Here is the .proto file that defines your messages, addressbook.proto.

package tutorial;
import "php.proto";
option (php.package) = "Tutorial.AddressBookProtos";

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

message AddressBook {
  repeated Person person = 1;
}

As you can see, the syntax is similar to C++ or Java. Let's go through each part of the file and see what it does. The .proto file starts with a package declaration, which helps to prevent naming conflicts between different projects. In PHP, the package name is used as the PHP namespace unless you have explicitly specified a (php.package), as we have here. Even if you do provide a (php.package), you should still define a normal package as well to avoid name collisions in the Protocol Buffers name space as well as in non PHP languages.

After the package declaration, you can see two options that are PHP-specific: import "php.proto"; and (php.package).

Next, you have your message definitions. A message is just an aggregate containing a set of typed fields. Many standard simple data types are available as field types, including bool, int32, float, double, and string. You can also add further structure to your messages by using other message types as field types – in the above example the Person message contains PhoneNumber messages, while the AddressBook message contains Person messages. You can even define message types nested inside other messages – as you can see, the PhoneNumber type is defined inside Person. You can also define enum types if you want one of your fields to have one of a predefined list of values – here you want to specify that a phone number can be one of MOBILE, HOME, or WORK.

The " = 1", " = 2" markers on each element identify the unique tag that field uses in the binary encoding. Tag numbers 1-15 require one less byte to encode than higher numbers, so as an optimization you can decide to use those tags for the commonly used or repeated elements, leaving tags 16 and higher for less-commonly used optional elements. Each element in a repeated field requires re-encoding the tag number, so repeated fields are particularly good candidates for this optimization.

Each field must be annotated with one of the following modifiers:

You'll find a complete guide to writing .proto files – including all the possible field types – in the Protocol Buffer Language Guide. Don't go looking for facilities similar to class inheritance, though – protocol buffers don't do that.

Compiling Your Protocol Buffers

Now that you have a .proto, the next thing you need to do is generate the classes you'll need to read and write AddressBook (and hence Person and PhoneNumber) messages. To do this, you need to run the protocol buffer plugin on your .proto:

If you haven't installed the compiler (protoc) or you dont have the php plugin, see https://github.com/protobuf-php/protobuf-plugin.

Now run the compiler plugin, specifying the proto files source directory (the file directory is used if you don't provide a value), the destination directory (where you want the generated code to go), and the path to your .proto In this case:

php ./vendor/bin/protobuf --include-descriptors -i . -o ./src/ ./addressbook.proto

This generates the following PHP classes in your specified destination directory

src/
└── Tutorial
    └── AddressBookProtos
        ├── AddressBook.php
        ├── Person
        │   ├── PhoneNumber.php
        │   └── PhoneType.php
        └── Person.php

The Protocol Buffer API

Let's look at some of the generated code and see what classes and methods the compiler has created for you. If you look in src/Tutorial/AddressBookProtos/Person.php you can see that it defines a class called Person.

Messages have auto-generated accessor methods for each field of the message. Here are some of the accessors for the Person class (implementations omitted for brevity):

<?php
###################### required string name = 1; ###################################
/** @return bool */
public function hasName();
/** @return string */
public function getName();
/** @param string $value */
public function setName($value);
####################################################################################


###################### required int32 id = 2; ######################################
/** @return bool */
public function hasId();
/** @return int */
public function getId();
/** @param int $value */
public function setId($value);
####################################################################################


###################### optional string email = 3; ##################################
/** @return bool */
public function hasEmail();
/** @return string */
public function getEmail();
/** @param string $value */
public function setEmail($value);
####################################################################################


###################### repeated .tutorial.Person.PhoneNumber phone = 4; ############
/** @return bool */
public function hasPhoneList();
/** @return \Protobuf\Collection<\ProtobufTest\Protos\Person\PhoneNumber> */
public function getPhoneList();
/** @param \Protobuf\Collection<\ProtobufTest\Protos\Person\PhoneNumber> $value */
public function setPhoneList(\Protobuf\Collection $value);
####################################################################################
?>

As you can see, there are simple getters and setters for each field. There are also has getters for each singular field which return true if that field has been set. Repeated fields have a extra method, an add method which appends a new element to the list.

Notice how these accessor methods use camel-case naming, even though the .proto file uses lowercase-with-underscores. This transformation is done automatically by the protocol buffer compiler so that the generated classes match standard PHP style conventions. You should always use lowercase-with-underscores for field names in your .proto files; this ensures good naming practice in all the generated languages. See the style guide for more on good .proto style.

Protocol Buffers types map to the following PHP types:

Protocol BuffersPHP
doublefloat
floatfloat
int32int
int64int
uint32int
uint64int
sint32int
sint64int
fixed32int
fixed64int
sfixed32int
sfixed64int
boolbool
stringstring
bytes\Protobuf\Stream

Enums and Nested Classes

The generated code includes a PhoneType enum:

<?php
namespace Tutorial\AddressBookProtos\Person;

class PhoneType extends \Protobuf\Enum
{
    /**
     * @return \Tutorial\AddressBookProtos\Person\PhoneType
     */
    public static function MOBILE() { /** ... */ }

    /**
     * @return \Tutorial\AddressBookProtos\Person\PhoneType
     */
    public static function HOME() { /** ... */ }

    /**
     * @return \Tutorial\AddressBookProtos\Person\PhoneType
     */
    public static function WORK() { /** ... */ }
?>

All nested types are generated using the parent class Person as part of its namespace.

<?php
use Tutorial\AddressBookProtos\Person;

$person = new Person();
$phone  = new Person\PhoneNumber();
$type   = Person\PhoneType::MOBILE();

$person->setId(1);
$person->setName('Fabio B. Silva');
$person->setEmail('fabio.bat.silva@gmail.com');

$phone->setType($type);
$phone->setNumber('1231231212');
?>

Known issues

Parsing and Serialization

Each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include :

<?php

/**
 * Message constructor
 *
 * @param \Protobuf\Stream|resource|string $stream
 * @param \Protobuf\Configuration          $configuration
 */
public function __construct($stream = null, Configuration $configuration = null);

/**
 * Creates message from the given stream.
 *
 * @param \Protobuf\Stream|resource|string $stream
 * @param \Protobuf\Configuration          $configuration
 *
 * @return \Protobuf\Message
 */
public static function fromStream($stream, Configuration $configuration = null);


/**
 * Serializes the message and returns a stream containing its bytes.
 *
 * @param \Protobuf\Configuration $configuration
 *
 * @return \Protobuf\Stream
 */
public function toStream(Configuration $configuration = null);

/**
 * Returns a human-readable representation of the message, particularly useful for debugging.
 *
 * @return string
 */
public function __toString();
?>

Writing A Message

Now let's try using your protocol buffer classes. The first thing you want your address book application to be able to do is write personal details to your address book file. To do this, you need to create and populate instances of your protocol buffer classes and then write them to an output stream.

Here is a program which reads an AddressBook from a file, adds one new Person to it based on user input, and writes the new AddressBook back out to the file again. The parts which directly call or reference code generated by the protocol compiler are highlighted.

#!/usr/bin/env php
<?php

use Tutorial\AddressBookProtos\Person;
use Tutorial\AddressBookProtos\AddressBook;

// Read the existing address book or create a new one.
$addressBook = is_file($argv[1])
    ? new AddressBook(file_get_contents($argv[1]))
    : new AddressBook();

$person = new Person();
$id     = intval(readline("Enter person ID: "));
$name   = trim(readline("Enter person name: "));
$email  = trim(readline("Enter email address (blank for none): "));

$person->setId($id);
$person->setName($name);

if ( ! empty($email)) {
    $person->setEmail($email);
}

while (true) {
    $number = trim(readline("Enter a phone number (or leave blank to finish):"));

    if (empty($number)) {
        break;
    }

    $phone  = new Person\PhoneNumber();
    $type   = trim(readline("Is this a mobile, home, or work phone? "));

    switch (strtolower($type)) {
        case 'mobile':
            $phone->setType(Person\PhoneType::MOBILE());
            break;
        case 'work':
            $phone->setType(Person\PhoneType::WORK());
            break;
        case 'home':
            $phone->setType(Person\PhoneType::HOME());
            break;
        default:
            echo "Unknown phone type. Using default." . PHP_EOL;
    }

    $phone->setNumber($number);
    $person->addPhone($phone);
}

// Add a person.
$addressBook->addPerson($person);

// Print current address book
echo $addressBook;

// Write the new address book back to disk.
file_put_contents($argv[1], $addressBook->toStream());
?>

This tutorial documentation its based on the Protocol Buffer Basics Tutorial.