- var_dump: documentation ( source)
- unserialize: documentation ( source)
- serialize: documentation ( source)
<?php
// 'Child' object.
class Child
{
public $property = 'foo';
}
// 'Parent' object.
class ParentA implements Serializable
{
public $rofl = 'foo';
public function serialize()
{
return serialize(array($this->rofl));
}
public function unserialize($data)
{
list($this->rofl) = unserialize($data);
}
}
class ParentB extends ParentA
{
public $collection;
public function __construct(array $items)
{
$this->collection = array();
foreach ($items as $item)
{
$this->collection[] = $item;
}
}
public function serialize()
{
return serialize(array(
'foo' => $this->collection,
'parent' => parent::serialize()
));
}
public function unserialize($data)
{
$data = unserialize($data);
$this->collection = $data['foo'];
parent::unserialize($data['parent']);
}
}
// Wrapper object.
class Wrapper
{
public $parent;
public $children;
public function __construct(ParentB $parent, array $children)
{
$this->parent = $parent;
$this->children = array();
foreach ($children as $child)
{
$this->children[] = $child;
}
}
}
$children = [new Child(), new Child(), new Child()];
$parent = new ParentB($children);
$wrapper = new Wrapper($parent, $children);
$data = serialize($wrapper);
var_dump(unserialize($data));