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 unseria
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'];
}
}
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.