<?php
class Period implements JsonSerializable
{
private $startDate;
private $endDate;
public function __construct()
{
$this->startDate = new DateTimeImmutable('2015-04-03');
$this->endDate = new DateTimeImmutable('2015-05-03');
}
public function jsonSerialize()
{
return [
'startDate' => $this->startDate,
'endDate' => $this->endDate,
];
}
}
$period = new Period();
var_dump(json_decode(json_encode($period)));
Deprecated: Return type of Period::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4pV59 on line 15
object(stdClass)#5 (2) {
["startDate"]=>
object(stdClass)#4 (3) {
["date"]=>
string(26) "2015-04-03 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
["endDate"]=>
object(stdClass)#6 (3) {
["date"]=>
string(26) "2015-05-03 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
}