Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Protobuf Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package via Composer: composer require google/protobuf. Ensure the PECL protobuf extension is installed for best performance (fallback to pure PHP runtime if missing).
  2. Write your first .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;
    }
    
  3. Generate PHP classes using the protoc compiler (bundled or installed separately):
    protoc --php_out=generated user.proto
    
    This creates generated/Google/Protobuf/Type/User.php with strongly-typed classes.
  4. Use generated classes in PHP:
    use Myapp\User;
    $user = new User();
    $user->setId('123')->setEmail('alice@example.com')->setIsActive(true);
    $bytes = $user->serializeToString();
    $parsed = User::parse($bytes);
    

Implementation Patterns

  • Microservices Communication: Define shared .proto contracts used across services (e.g., gRPC or REST+Protobuf). Share .proto files in a dedicated repo to enforce interface contracts.
  • Event Sourcing / Kafka: Serialize domain events to compact binary format for efficient storage/transport; use Oneof and Any types for polymorphic payloads.
  • API Versioning: Use oneof and field presence (FieldMask) to safely deprecate fields without breaking clients; embed version metadata in message headers.
  • Bundle & Stream: Use BytesValue/StringValue wrappers or Stream helpers for large payloads (e.g., file uploads via streaming RPCs).
  • Test-Driven Schema Design: Write unit tests around serialization behavior (e.g., assertEquals($original, User::parse($original->serializeToString()))) to catch breaking changes early.

Gotchas and Tips

  • PECL vs. Pure PHP: The pure-PHP runtime is slower and memory-heavy—always prefer the PECL extension (ext-protobuf). Check via class_exists('\Google\Protobuf\Internal\Descriptor').
  • File Paths & Namespaces: Generated code uses PSR-4-like structure (Google\Protobuf\Internal\*). Update autoloading (composer.json"autoload": { "psr-4": { "Myapp\\": "generated/" } }) to match your package and php_namespace.
  • Enum Quirks: Enums are not native PHP enums—use MyEnum::getValue('VALUE_NAME') and check isValidValue(); deprecated enum values won’t cause runtime errors.
  • Backward Compatibility: Avoid renumbering fields or deleting required fields (all fields are optional in 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; }).
  • Debugging: Enable Google\Protobuf\Internal\Message::debugToString() to inspect message state, or use Json::encode($message) for human-readable logs.
  • IDE Support: Use generated docblocks + PHPStan/Psalm level 5+ for robust validation of message properties (e.g., $user->getEmail() won’t type-check without docblock hints in some IDEs).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport