<?php
class Model
{
protected $comments;
public function __construct()
{
$this->comments = new class {
public function count()
{
return rand(1, 20);
}
};
}
}
class CommentCount
{
private $count;
public function __construct(int $count)
{
$this->count = $count;
}
public function getCount(): int
{
return $this->count;
}
}
class Post extends Model
{
public function commentCount(): CommentCount
{
return new CommentCount($this->comments->count());
}
}
$post = new Post();
$commentCount = $post->commentCount();
var_dump($commentCount->getCount());
var_dump($commentCount->getCount());
var_dump($commentCount->getCount());
var_dump($commentCount->getCount());
var_dump($commentCount->getCount());