在proto3中,可以直接使用protoc
命令生成PHP代码。生成的PHP代码不能直接使用,还需要 Protobuf 的PHP库 支持。
下面通过一个例子演示下PHP怎么使用protobuf。首先定义proto文件:
syntax = "proto3";
package lm;
message helloworld
{
int32 id = 1; // ID
string str = 2; // str
int32 opt = 3; // optional field
}
注意这里采用的是proto3的语法,和proto2不太一样,required
和optional
的限定已经没有了,所有的字