- json_encode: documentation ( source)
<?php
class Foo implements Serializable {
public function jsonSerialize() {
return [
'title' => $this->title,
'parent' => $this->parent,
];
}
}
$f1 = new Foo();
$f1->title = 'Foo';
$f2 = new Foo();
$f2->title = 'Bar';
$f2->parent = $f1;
echo json_encode($f2);