- json_encode: documentation ( source)
<?php
class A implements JsonSerializable{
public $baz = 10;
function jsonSerialize(){
return ['bar' => 20];
}
}
class B implements JsonSerializable{
function jsonSerialize(){
return ['foo' => new A];
}
}
echo json_encode(new B, JSON_PRETTY_PRINT);