3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); final class CreateStatementRequest { private ?DateTimeImmutable $startDate, $endDate; public function __construct( DateTimeImmutable $startDate = null, DateTimeImmutable $endDate = null ) { $this->startDate = $startDate; $this->endDate = $endDate; // error: Property CreateStatementRequest::$endDate (DateTimeImmutable) does not accept DateTimeImmutable|null. } public function hasFilterStartDate(): bool { return $this->startDate !== null; } public function getFilterStartDate(): DateTimeImmutable { if ($this->startDate === null) { throw new RuntimeException('The start date filter is not defined.'); } return $this->startDate; } public function hasFilterEndDate(): bool { return $this->endDate !== null; } public function getFilterEndDate(): DateTimeImmutable { if ($this->endDate === null) { // error: Strict comparison using === between DateTimeImmutable and null will always evaluate to false. throw new RuntimeException('The end date filter is not defined.'); } return $this->endDate; } } $ref = new \ReflectionClass(CreateStatementRequest::class); foreach ($ref->getProperties() as $refProperty) { var_dump($refProperty->getName()); $refType = $refProperty->getType(); var_dump($refType->getName()); var_dump($refType->allowsNull()); var_dump('---'); }
Output for 7.4.3 - 7.4.25, 8.0.0 - 8.0.12, 8.1rc1 - rc3
string(9) "startDate" string(17) "DateTimeImmutable" bool(true) string(3) "---" string(7) "endDate" string(17) "DateTimeImmutable" bool(true) string(3) "---"
Output for 7.4.0 - 7.4.2
string(9) "startDate" string(17) "DateTimeImmutable" bool(true) string(3) "---" string(7) "endDate" string(17) "DateTimeImmutable" bool(false) string(3) "---"
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31
Parse error: syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST) in /in/hITTk on line 6
Process exited with code 255.

preferences:
41.21 ms | 462 KiB | 5 Q