3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CommentBase { 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 getUserId() : int { return $this->userId; } public function getText() : string { return $this->text; } public function setText(string $text) { $this->text = $text; } } interface Entity { public function getId() : int; } class Comment extends CommentBase implements Entity { private int $id; public function __construct(int $id, int $userId, string $text) { $this->id = $id; parent::__construct($userId, $text); } public function getId() : int { return $this->id; } } class CommentRepository { private array $entities = []; public function create(CommentBase $comment): Comment { $id = $this->generateId(); $entities[$id] = new Comment($id, $comment->getUserId(), $comment->getText()); 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(new CommentBase(11, 'Test comment')); $comment->setText("updated comment"); $comment = $repo->update($comment); var_dump($comment);
based on B1PQL