3v4l.org

run code in 200+ PHP versions simultaneously
<?php interface Entity { public function getId() : int; } class Comment implements Entity { private int $id; private int $userId; private string $text; public function __construct(int $id, int $userId, string $text) { $this->id = $id; $this->userId = $userId; $this->text = $text; } public function getId() : int { return $this->id; } public function getUserId() : int { return $this->userId; } public function getText() : string { return $this->text; } public function setText(string $text): void { $this->text = $text; } } class CommentRepository { private array $entities = []; public function create(int $userId, string $text): Comment { $id = $this->generateId(); $entities[$id] = new Comment($id, $userId, $text); return $entities[$id]; } public function update(Comment $comment): Comment { $this->entities[$comment->getId()] = $comment; return $this->entities[$comment->getId()]; } private function generateId() : int { return random_int(1, 1000000); } } $repo = new CommentRepository; $comment = $repo->create(11, 'Test comment'); $comment->setText("updated comment"); $comment = $repo->update($comment); var_dump($comment);
based on B1PQL
Output for 7.4.1
object(Comment)#2 (3) { ["id":"Comment":private]=> int(775775) ["userId":"Comment":private]=> int(11) ["text":"Comment":private]=> string(15) "updated comment" }
Output for 7.4.0
object(Comment)#2 (3) { ["id":"Comment":private]=> int(722897) ["userId":"Comment":private]=> int(11) ["text":"Comment":private]=> string(15) "updated comment" }
Output for 7.2.0 - 7.3.13
Parse error: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/FX4Ms on line 10
Process exited with code 255.