3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface Entity { public function getId() : ?int; } class Comment implements Entity { private ?int $id = null; private int $userId; private string $text; public function __construct(int $userId, string $text) { $this->userId = $userId; $this->text = $text; } public function getId() : ?int { return $this->id; } public function setId(int $id) : void { $this->id = $id; } } class PseudoDatabase { private array $entities = []; public function save(Entity $entity) { $id = $entity->getId(); if (null === $id) { $id = $this->generateId(); $entity->setId($id); } $this->entities[$id] = $entity; } private function generateId() : int { return random_int(1, 1000000); } } $comment = new Comment(11, 'Test comment'); $database = new PseudoDatabase; $database->save($comment); var_dump($comment);
Output for 7.4.1
object(Comment)#1 (3) { ["id":"Comment":private]=> int(652247) ["userId":"Comment":private]=> int(11) ["text":"Comment":private]=> string(12) "Test comment" }
Output for 7.4.0
object(Comment)#1 (3) { ["id":"Comment":private]=> int(269900) ["userId":"Comment":private]=> int(11) ["text":"Comment":private]=> string(12) "Test comment" }
Output for 7.2.0 - 7.2.26, 7.3.0 - 7.3.13
Parse error: syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST) in /in/B1PQL on line 9
Process exited with code 255.

preferences:
76.18 ms | 401 KiB | 48 Q