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;
}
}
}
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.
How can I solve that?
Maybe I can use the var_dump() output, but how I can store it into a variable?