@ 2020-01-14T13:59:12Z <?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);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.4.1 object(Comment)#3 (3) {
["id":"Comment":private]=>
int(595291)
["userId":"CommentBase":private]=>
int(11)
["text":"CommentBase":private]=>
string(15) "updated comment"
}
Output for 7.4.0 object(Comment)#3 (3) {
["id":"Comment":private]=>
int(671753)
["userId":"CommentBase":private]=>
int(11)
["text":"CommentBase":private]=>
string(15) "updated comment"
}
Output for 7.2.0 - 7.2.26 , 7.3.0 - 7.3.13 Parse error: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/k7CDM on line 5
Process exited with code 255 . preferences:dark mode live preview
72.59 ms | 402 KiB | 48 Q