<?php
class A implements JsonSerializable{
public $baz = 10;
function jsonSerialize(){
return ['bar' => 20];
}
}
class B implements JsonSerializable{
function jsonSerialize(){
return ['foo' => (new A)->jsonSerialize()];
}
}
echo json_encode(new B, JSON_PRETTY_PRINT);
Deprecated: Return type of A::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/hPEHQ on line 5
Deprecated: Return type of B::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/hPEHQ on line 11
{
"foo": {
"bar": 20
}
}