Quickly teach you how to use protobuf in php

藏色散人
Release: 2023-04-10 19:54:02
forward
5663 people have browsed it

Summary

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

In

github Search google/protobuf to find the official source library document, find the corresponding PHP document, and follow the official document to operate.

The installation in the document involves two parts, one is to install the C extension of

protobuf, and the other is the dependency package google/protobuf.

To install the C extension, use the

pecl command (you need to use find / -name pecl to find it, the installation path is not in the environment variable);

In addition, it also relies on other commands that need to be installed first. The path for downloading and installing the C extension does not meet expectations (my

php uses the library installation of remi-php, the path and The default settings of the original image are different);

After that, you need to establish a soft link to

protobuf.so;

After that, you can use

composer to install the dependency package .

Next, you need the code generator

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
Copy after login

Extension

Different versions of

protobuf 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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template