fork download
  1. <?php
  2.  
  3. class User
  4. {
  5. protected $id;
  6. protected $name;
  7. protected $age;
  8. protected $comments;
  9.  
  10. public function __construct()
  11. {
  12. $this->comments = new ArrayCollection();
  13. }
  14.  
  15. public function getRating()
  16. {
  17. return 100 * count($this->comments->toArray()) + 5 * $this->age;
  18. }
  19.  
  20. public function getId()
  21. {
  22. return $this->id;
  23. }
  24.  
  25. public function getName()
  26. {
  27. return $this->name;
  28. }
  29.  
  30. public function getAge()
  31. {
  32. return $this->age;
  33. }
  34.  
  35. public function getComments()
  36. {
  37. return $this->comments->toArray();
  38. }
  39.  
  40. public function setName($name)
  41. {
  42. $this->name = $name;
  43. }
  44.  
  45. public function setAge($age)
  46. {
  47. $this->age = $age;
  48. }
  49.  
  50. public function setComments(array $comments)
  51. {
  52. foreach ($comments as $c) {
  53. $this->comments[] = $c;
  54. }
  55. }
  56. }
  57.  
  58. class UserProxy extends User
  59. {
  60. private $repository;
  61.  
  62. public function __construct($id)
  63. {
  64. $this->repository = new UserRepository;
  65. $this->id = $id;
  66. }
  67.  
  68. public function getName()
  69. {
  70. if ($this->name === null) {
  71. return $this->repository->findById($this->id)->getName();
  72. }
  73. parent::getName();
  74. }
  75.  
  76. public function getAge()
  77. {
  78. if ($this->age === null) {
  79. return $this->repository->findById($this->id)->getAge();
  80. }
  81. parent::getAge();
  82. }
  83.  
  84. public function getComments()
  85. {
  86. if ($this->comments === null) {
  87. return $this->repository->findById($this->id)->getComments();
  88. }
  89. parent::getComments();
  90. }
  91.  
  92. public function getRating()
  93. {
  94. if ($this->raging === null) {
  95. return $this->repository->findById($this->id)->getRating();
  96. }
  97. parent::getComments();
  98. }
  99. }
  100.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Standard output is empty