PHP序列化对象树 [英] PHP Serialize object tree
问题描述
我有一个类似于以下的对象树,我需要对其进行序列化并将其存储在文件系统上.我需要具有所有类属性的完整层次结构,稍后我将反序列化并还原类层次结构.
I have an object tree like the following, which I need to serialize and store on the filesystem. I need the full hierarchy with all class properties and later I will unserialize and restore the class hierarchy.
class X implements \Serializable {
private $x1;
public function serialize() {
return serialize(get_class_vars(get_class($this)));
}
public function unserialize($data) {
$values = unserialize($data);
foreach ($values as $key => $value) {
$this->$key = $value;
}
}
}
class A implements \Serializable {
private $a1;
private $a2;
// type of a3 is class X!
protected $a3;
public function serialize() {
return serialize(get_class_vars(get_class($this)));
}
public function unserialize($data) {
$values = unserialize($data);
foreach ($values as $key => $value) {
$this->$key = $value;
}
}
}
class B extends A implements \Serializable {
private $b1;
private $b2;
public function serialize() {
// $base = parent::serialize();
return serialize(get_class_vars(get_class($this)));
}
public function unserialize($data) {
$values = unserialize($data);
foreach ($values as $key => $value) {
$this->$key = $value;
}
}
}
class C extends A implements \Serializable {
private $c1;
private $c2;
public function serialize() {
// $base = parent::serialize();
return serialize(get_class_vars(get_class($this)));
}
public function unserialize($data) {
$values = unserialize($data);
foreach ($values as $key => $value) {
$this->$key = $value;
}
}
}
子类可以序列化自身,但是对于我不知道的基类,我如何组合序列化的数据.此外,我从文件系统中获取序列化数据,但我不知道我将获得哪个子类. PHP的unserialize()是否创建正确的类实例?它还应该初始化基类A.
The subclasses can serialize itself, but for the base class I don't know, how I can combine the serialized data. Furthermore I get serialized data from the filesystem, but I don't know, which subclass I will get. Does PHP's unserialize() create the right class instance? It should also initialize the base class A.
我该如何解决?
也许我可以使用var_dump()输出,但是如何将其存储到变量中?
Maybe I can use the var_dump() output, but how I can store it into a variable?
推荐答案
这是我建议序列化对象的方式:
This is how I would recommend serializing objects:
class Color implements \Serializable
{
private $Name;
private $Type;
public function __construct(string $Name, int $Type)
{
$this->Name = $Name;
$this->Type = $Type;
}
public function serialize()
{
$Props['Name'] = $this->Name;
$Props['Type'] = $this->Type;
return serialize($Props);
}
public function unserialize($Data)
{
list($this->Name, $this->Type) = unserialize($Data);
}
}
class Blue extends Color
{
private $Intensity;
public function __construct()
{
parent::__construct('Blue', 10);
$this->Intensity = 90;
}
public function serialize()
{
$Props['parent'] = parent::serialize();
$Props['Intensity'] = $this->Intensity;
return serialize($Props);
}
public function unserialize($Data)
{
$Obj = unserialize($Data);
parent::unserialize($Obj['parent']);
$this->Intensity = $Obj['Intensity'];
}
}
传递给serialize()函数的任何对象都是您将以字符串形式返回到unserialize()的对象.如果走了这条路,则可以在trait中实现serialize()/unserialize()函数,并且get_object_vars()对于私有变量将正常工作.
Whichever object you pass in to the serialize() function is the object you will get back (as a string) to unserialize(). If you go your route, then you can implement the serialize()/unserialize() functions inside a trait and get_object_vars() will work properly for private variables.
这篇关于PHP序列化对象树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!