Home > Article > Backend Development > Quickly teach you how to use protobuf in php
Here is an operation tutorial, recorded for subsequent review.
Using protobuf(v3)
in PHP for serialization and deserialization, what steps are required from installation to use, and what issues should be paid attention to.
Operating environment
Centos7.8
php7.4
##Installation
Ingithub Search
google/protobuf to find the official source library document, find the corresponding PHP document, and follow the official document to operate.
protobuf, and the other is the dependency package
google/protobuf.
pecl command (you need to use
find / -name pecl to find it, the installation path is not in the environment variable);
php uses the library installation of
remi-php, the path and The default settings of the original image are different);
protobuf.so;
composer to install the dependency package .
protoc, [proto installation], parse the definition file
*.proto and generate the corresponding
php code , to use it in the project, you need to introduce the corresponding code. You need to change the
composer.json file and add the
autoload configuration. If it is a test, you can also manually
require .
Usage
Usage is mainly divided into two points, one is serialization and the other is deserialization. [Recommended:PHP Video Tutorial]
$pb=new Demo(); $pb->setName('demo'); //序列化,数据不可看 $string=$pb->serializeToString(); //序列化,数据可看 $string=$pb->serializeToJsonString(); //反序列化 $pb=new Demo(); //从db中获取到的序列化值,反序列化赋值给$pb,然后就可以正常使用 $pb->mergeFromString($string); $pb->mergeFromJsonString($string); $pb->getName();//输出demo
ExtensionDifferent versions ofprotobuf
are not compatible. If version conversion is involved, You can refer to the blog [Using protobuf in php] PHP documentation PHP documentation
: https://github.com/protocolbuffers/protobuf/tree/master/php ##
The above is the detailed content of Quickly teach you how to use protobuf in php. For more information, please follow other related articles on the PHP Chinese website!