3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ClockInterface { public function now(): DateTimeImmutable; } class BadMockClock implements ClockInterface { public function __construct(private readonly DateTimeImmutable $now = new DateTimeImmutable()) { } public function now(): DateTimeImmutable { return $this->now; } } class GoodMockClock implements ClockInterface { public function __construct(private readonly DateTimeImmutable $now = new DateTimeImmutable()) { } public function now(): DateTimeImmutable { return clone $this->now; } } class Clock implements ClockInterface { public function now(): DateTimeImmutable { return new DateTimeImmutable(); } } $clocks = [ new BadMockClock(), new GoodMockClock(), new Clock(), ]; shuffle($clocks); foreach ($clocks as $i => $clock) { $t1 = $clock->now(); $t2 = $clock->now(); if ($t1 === $t2) { echo "Clock mock leaks at index $i\n"; } }
Output for 8.2.5
Clock mock leaks at index 0

preferences:
154.48 ms | 1398 KiB | 8 Q