- var_dump: documentation ( source)
- random_int: documentation ( source)
<?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);