3v4l.org

run code in 300+ PHP versions simultaneously
<?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([ '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));

preferences:
41.41 ms | 402 KiB | 5 Q