3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class ContractVersion { private $end_date; public function __construct(\DateTimeImmutable $end_date) { $this->end_date = $end_date; } public function getEndDate(): \DateTimeImmutable { return $this->end_date; } } class VersionedContract { private $versions = []; public function __construct(\DateTimeImmutable $end_date) { $this->versions[] = new ContractVersion($end_date); } public function getEndDate(): \DateTimeImmutable { return $this->getCurrentVersion()->getEndDate(); } private function getCurrentVersion(): ContractVersion { return end($this->versions); } public function renew(\DateInterval $interval) { $this->versions[] = new ContractVersion($this->getEndDate()->add($interval)); } } $contract = new VersionedContract(new \DateTime('2017-10-10 10:10:00')); var_dump($contract->getEndDate()); // string(26) "2017-10-10 10:10:00.000000" $endDate = $contract->getEndDate(); var_dump($endDate); // same string(26) "2017-10-10 10:10:00.000000" $endDate->add(new \DateInterval('P1Y')); var_dump($endDate); // "2017-10-10 10:10:00.000000" var_dump($contract->getEndDate());