5.35-dev) is available as a pure PHP Composer package or as a native C extension via PECL. Both provide the same API and share generated code — you can switch implementations without regenerating.
Installation
- Composer (pure PHP)
- PECL (C extension)
Generating code
Installprotoc and run it with --php_out to generate PHP classes:
addressbook.proto with package tutorial, this creates:
GPBMetadata/Addressbook.php— metadata for the descriptor poolTutorial/Person.php,Tutorial/AddressBook.php, etc. — message classesTutorial/Person/PhoneNumber.php,Tutorial/Person/PhoneType.php— nested types
Working with messages
Require the generated files and use the classes directly:Serializing and parsing
Known limitations
Key API reference
Message methods
Every generated message class provides
serializeToString(), mergeFromString($data), serializeToJsonString(), and mergeFromJsonString($json).Field accessors
Fields are accessed via generated getters and setters:
getName() / setName($value). Repeated fields return a RepeatedField object; map fields return a MapField.Enums
Enum values are PHP integer constants. For example,
Tutorial\Person\PhoneType::MOBILE is 0.Descriptor pool
Metadata files (under
GPBMetadata/) register message descriptors with the global descriptor pool when required. Always require them before using the message classes.