定制 robertlabrie/robertserializer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robertlabrie/robertserializer

Composer 安装命令:

composer require robertlabrie/robertserializer

包简介

My serializer

README 文档

README

Why

RobertSerializer was born out of the desire to easily store objects in MongoDB. The existing PHP serializer is fine for storing in session, but is useless for a document DB. When I asked around, the consensus seemed to be using something called an "ODM (Object Document Model)" which is essentially a property map between the Mongo document and my PHP class. I didn't feel like maintaining one of those! So, RobertSerializer is what I came up with.

How it works

I leverage PHPs' reflection class to recursively crawl the input object properties and feed the data back out as an array. Mongo likes arrays, so that's why it's an array, but you could easily called json_encode() on it and store it in RavenDB if that's your thing.

What's the catch?

I need to know your objects type when I deserialize later, so I add an extra element to the array __TYPE__ (you can change it). For this reason, it's a good idea to use namespaces. Also, probably if you change your class namespace, you won't be able to deserialize an object. If anyone uses this and that's a problem, we'll let you define a map (boo!) so that you can survive a refactoring.

Why a special field?

BSON seemed ugly

How to use it

composer require RobertSerializer

#do the necessary composer bits
$rs = new \RobertSerializer\RobertSerializer();
$in = new \Example\Foo();
$ser = $rs->serialize($in);
//$ser is an array that you can pass around

$out = $rs->deserialize($ser);
//$out is an instance of Foo(), exactly how you remember it!

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL
  • 更新时间: 2015-07-08