/** * get descriptor for protocol buffers * * @return array */ public static function getDescriptor() { if (!isset(self::$descriptor)) { $desc = new ProtocolBuffersDescriptorBuilder(); $desc->addField(1, new ProtocolBuffersFieldDescriptor(array("type" => ProtocolBuffers::TYPE_ENUM, "name" => "type", "packable" => false, "repeated" => false, "default" => null))); $desc->addField(2, new ProtocolBuffersFieldDescriptor(array("type" => ProtocolBuffers::TYPE_STRING, "name" => "number", "packable" => false, "repeated" => false, "default" => null))); self::$descriptor = $desc->build(); } return self::$descriptor; }
<?php require dirname(__FILE__) . DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, array("php", "addressbook.proto.php")); $db = "addressbook.db"; $addressbook = load($db); $person = new Tutorial_Person(); $person->setId(1); $person->setName("John Doe"); $person->setEmail("*****@*****.**"); $phone = new Tutorial_Person_PhoneNumber(); $phone->setNumber("1234 5 67 78"); $phone->setType(Tutorial_Person_PhoneType::HOME); $person->appendPhone($phone); $phone = new Tutorial_Person_PhoneNumber(); $phone->setNumber("1234 5 67 79"); $phone->setType(Tutorial_Person_PhoneType::HOME); $person->appendPhone($phone); echo $person; exit; echo "____________________\n"; $addressbook->appendPerson($person); var_dump($addressbook); save($db, $addressbook); exit; function load($path) { $data = @file_get_contents($path); try { $result = ProtocolBuffers::decode("Tutorial_AddressBook", $data); } catch (ProtocolBuffersInvalidProtocolBufferException $e) { $result = new Tutorial_AddressBook();