php与protoBuffer入门例子

1,先创建一个test_my.proto 文件(.proto)定义数据对象类型

如下:


message Tree
{
   required string name = 1;
   required int32 id = 2;
   optional string height = 3;
}

 

2,新建一个php运行编译上面的数据对象,使之生成pb_proto_test_my.php,里面生成上方的Tree一个类信息

 

编译代码如下:

require_once('../parser/pb_parser.php');

$test = new PBParser();
$test->parse('./test_my.proto');
var_dump('File parsing done!');

 

3,新建一个test_my.pb(.pb)的文件用来序列化存储数据和读取数据

 

存储数据和读取数据代码如下:

// first include pb_message
require_once('../message/pb_message.php');

// now read it with the old file
// include the generated file
require_once('./pb_proto_test_my.php');

$string = file_get_contents('./test_my.pb');

// Just read it
$tree = new Tree();
$tree->set_name('2323');

$string = $tree->SerializeToString();

var_dump($tree->name());

file_put_contents('test_my.pb', $string);


demo example

http://download.csdn.net/detail/huyuantai000/5539987


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值