Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <?php
  2.  
  3. class Person
  4. {
  5. /**
  6. * @var string
  7. */
  8. private $name;
  9.  
  10. /**
  11. * @var int
  12. */
  13. private $age;
  14.  
  15. /**
  16. * Person constructor.
  17. * @param string $name
  18. * @param int $age
  19. * @throws Exception
  20. */
  21. public function __construct(string $name, int $age)
  22. {
  23. $this->setName($name);
  24. $this->setAge($age);
  25. }
  26.  
  27. /**
  28. * @return string
  29. */
  30. public function getName(): string
  31. {
  32. return $this->name;
  33. }
  34.  
  35. /**
  36. * @param string $name
  37. * @throws Exception
  38. */
  39. protected function setName(string $name): void
  40. {
  41. if ($name < 3) {
  42. throw new Exception("Name's length should not be less than 3 symbols!");
  43. }
  44. $this->name = $name;
  45. }
  46.  
  47. /**
  48. * @return int
  49. */
  50. public function getAge(): int
  51. {
  52. return $this->age;
  53. }
  54.  
  55. /**
  56. * @param int $age
  57. * @throws Exception
  58. */
  59. protected function setAge(int $age): void
  60. {
  61. if ($age < 1) {
  62. throw new Exception("Age must be positive!");
  63. }
  64. $this->age = $age;
  65. }
  66.  
  67. }
  68.  
  69. ////////////////////////
  70. class Child extends Person
  71. {
  72. /**
  73. * @var string
  74. */
  75. private $name;
  76.  
  77. /**
  78. * @var int
  79. */
  80. private $age;
  81.  
  82. /**
  83. * Child constructor.
  84. * @param string $name
  85. * @param int $age
  86. * @throws Exception
  87. */
  88. public function __construct(string $name, int $age)
  89. {
  90. parent::__construct($name, $age);
  91. }
  92.  
  93. /**
  94. * @return string
  95. */
  96. public function getName(): string
  97. {
  98. return $this->name;
  99. }
  100.  
  101. /**
  102. * @param string $name
  103. * @throws Exception
  104. */
  105. protected function setName(string $name): void
  106. {
  107. if ($name < 3) {
  108. throw new Exception("Name's length should not be less than 3 symbols!");
  109. }
  110. $this->name = $name;
  111. }
  112.  
  113. /**
  114. * @return int
  115. */
  116. public function getAge(): int
  117. {
  118. return $this->age;
  119. }
  120.  
  121. /**
  122. * @param int $age
  123. * @throws Exception
  124. */
  125. protected function setAge(int $age): void
  126. {
  127. if ($age >= 15) {
  128. throw new Exception("Child's age must be less than 16!");
  129. }
  130. $this->age = $age;
  131. }
  132.  
  133. }
  134.  
  135. //////////////////////////
  136. $name = readline();
  137. $age = intval(readline());
  138.  
  139. try {
  140. $class = new Person($name, $age);
  141. } catch (Exception $e) {
  142. echo $e->getMessage();
  143. return $e;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement