google/protobuf
PHP support files for Google Protocol Buffers via Composer. This repo mirrors the main protocolbuffers/protobuf project and exists for PHP installation. For issues, support, and development, use the upstream protobuf repository.
composer require google/protobuf. Ensure the PECL protobuf extension is installed for best performance (fallback to pure PHP runtime if missing)..proto file (e.g., user.proto) defining message structure:
syntax = "proto3";
package myapp;
message User {
string id = 1;
string email = 2;
bool is_active = 3;
}
protoc compiler (bundled or installed separately):
protoc --php_out=generated user.proto
This creates generated/Google/Protobuf/Type/User.php with strongly-typed classes.use Myapp\User;
$user = new User();
$user->setId('123')->setEmail('alice@example.com')->setIsActive(true);
$bytes = $user->serializeToString();
$parsed = User::parse($bytes);
.proto contracts used across services (e.g., gRPC or REST+Protobuf). Share .proto files in a dedicated repo to enforce interface contracts.Oneof and Any types for polymorphic payloads.oneof and field presence (FieldMask) to safely deprecate fields without breaking clients; embed version metadata in message headers.BytesValue/StringValue wrappers or Stream helpers for large payloads (e.g., file uploads via streaming RPCs).assertEquals($original, User::parse($original->serializeToString()))) to catch breaking changes early.ext-protobuf). Check via class_exists('\Google\Protobuf\Internal\Descriptor').Google\Protobuf\Internal\*). Update autoloading (composer.json → "autoload": { "psr-4": { "Myapp\\": "generated/" } }) to match your package and php_namespace.MyEnum::getValue('VALUE_NAME') and check isValidValue(); deprecated enum values won’t cause runtime errors.proto3). Use reserved "field_name" and reserved 3, 5 to 7 in .proto to prevent reuse.Any and Oneof: Use Any::pack()/unpack() for polymorphic payloads, and Oneof to express mutual exclusivity (e.g., oneof payload { string text = 1; bytes binary = 2; }).Google\Protobuf\Internal\Message::debugToString() to inspect message state, or use Json::encode($message) for human-readable logs.$user->getEmail() won’t type-check without docblock hints in some IDEs).How can I help you explore Laravel packages today?