- json_encode: documentation ( source)
<?php
class Foo implements JsonSerializable
{
private $thing;
public function __construct($thing)
{
$this->thing = $thing;
}
public function jsonSerialize()
{
return [
'thing' => $this->thing,
];
}
}
$foo = new Foo('Foo Bar');
echo json_encode(['the_foo' => $foo]);