1. Go to this page and download the library: Download protobuf-php/protobuf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
protobuf-php / protobuf example snippets
###################### unction hasName();
/** @return string */
public function getName();
/** @param string $value */
public function setName($value);
####################################################################################
###################### ing 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);
####################################################################################
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() { /** ... */ }
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('[email protected]');
$phone->setType($type);
$phone->setNumber('1231231212');
/**
* 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();
#!/usr/bin/env 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());