3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class ContractVersion { private $end_date; public function __construct(\DateTime $end_date) { $this->end_date = $end_date; } public function getEndDate(): \DateTime { return clone $this->end_date; } } class VersionedContract { private $versions = []; public function __construct(\DateTime $end_date) { $this->versions[] = new ContractVersion($end_date); } public function getEndDate(): \DateTime { 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('1 week'); var_dump($endDate);