3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyEntity { public ?int $id = null; public ?\DateTimeImmutable $updatedAt = null; public function setUpdatedAt(): self { if (null !== $this->id || null === $this->updatedAt) { $this->updatedAt = new DateTimeImmutable(); } return $this; } } // Insert without custom date $a = new MyEntity(); $a->setUpdatedAt(); var_dump($a); // Insert with custom date $b = new MyEntity(); $b->updatedAt = new \DateTimeImmutable('2022-09-16'); $b->setUpdatedAt(); var_dump($b); // Update $c = new MyEntity(); $c->id = 42; $c->updatedAt = new \DateTimeImmutable('2022-09-16'); $c->setUpdatedAt(); var_dump($c);

preferences:
25.11 ms | 404 KiB | 5 Q