<?php
class User
{
protected $id;
protected $name;
protected $age;
protected $comments;
public function __construct()
{
$this->comments = new ArrayCollection();
}
public function getRating()
{
return 100 * count($this->comments->toArray()) + 5 * $this->age; }
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getAge()
{
return $this->age;
}
public function getComments()
{
return $this->comments->toArray();
}
public function setName($name)
{
$this->name = $name;
}
public function setAge($age)
{
$this->age = $age;
}
public function setComments
(array $comments) {
foreach ($comments as $c) {
$this->comments[] = $c;
}
}
}
class UserProxy extends User
{
private $repository;
public function __construct($id)
{
$this->repository = new UserRepository;
$this->id = $id;
}
public function getName()
{
if ($this->name === null) {
return $this->repository->findById($this->id)->getName();
}
parent::getName();
}
public function getAge()
{
if ($this->age === null) {
return $this->repository->findById($this->id)->getAge();
}
parent::getAge();
}
public function getComments()
{
if ($this->comments === null) {
return $this->repository->findById($this->id)->getComments();
}
parent::getComments();
}
public function getRating()
{
if ($this->raging === null) {
return $this->repository->findById($this->id)->getRating();
}
parent::getComments();
}
}