Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Person
- {
- /**
- * @var string
- */
- private $name;
- /**
- * @var int
- */
- private $age;
- /**
- * Person constructor.
- * @param string $name
- * @param int $age
- * @throws Exception
- */
- public function __construct(string $name, int $age)
- {
- $this->setName($name);
- $this->setAge($age);
- }
- /**
- * @return string
- */
- public function getName(): string
- {
- return $this->name;
- }
- /**
- * @param string $name
- * @throws Exception
- */
- protected function setName(string $name): void
- {
- if ($name < 3) {
- throw new Exception("Name's length should not be less than 3 symbols!");
- }
- $this->name = $name;
- }
- /**
- * @return int
- */
- public function getAge(): int
- {
- return $this->age;
- }
- /**
- * @param int $age
- * @throws Exception
- */
- protected function setAge(int $age): void
- {
- if ($age < 1) {
- throw new Exception("Age must be positive!");
- }
- $this->age = $age;
- }
- }
- ////////////////////////
- class Child extends Person
- {
- /**
- * @var string
- */
- private $name;
- /**
- * @var int
- */
- private $age;
- /**
- * Child constructor.
- * @param string $name
- * @param int $age
- * @throws Exception
- */
- public function __construct(string $name, int $age)
- {
- parent::__construct($name, $age);
- }
- /**
- * @return string
- */
- public function getName(): string
- {
- return $this->name;
- }
- /**
- * @param string $name
- * @throws Exception
- */
- protected function setName(string $name): void
- {
- if ($name < 3) {
- throw new Exception("Name's length should not be less than 3 symbols!");
- }
- $this->name = $name;
- }
- /**
- * @return int
- */
- public function getAge(): int
- {
- return $this->age;
- }
- /**
- * @param int $age
- * @throws Exception
- */
- protected function setAge(int $age): void
- {
- if ($age >= 15) {
- throw new Exception("Child's age must be less than 16!");
- }
- $this->age = $age;
- }
- }
- //////////////////////////
- $name = readline();
- $age = intval(readline());
- try {
- $class = new Person($name, $age);
- } catch (Exception $e) {
- echo $e->getMessage();
- return $e;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement