Created
October 8, 2016 20:02
-
-
Save evolic/d84088de09713d22191b7485906cd798 to your computer and use it in GitHub Desktop.
Detrmining if person is Polish man or woman based on his/her name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Persons; | |
| class Person | |
| { | |
| CONST MAN = 'man'; | |
| CONST WOMAN = 'woman'; | |
| /** | |
| * @var string | |
| */ | |
| private $name; | |
| /** | |
| * @var string | |
| */ | |
| private $gender; | |
| public function __construct($name) | |
| { | |
| $this->name = $name; | |
| $this->gender = $this->isMan($name) ? self::MAN : self::WOMAN; | |
| } | |
| protected function isMan($name) | |
| { | |
| return substr($name, -1, 1) !== 'a'; | |
| } | |
| private function setGender($gender) | |
| { | |
| $this->gender = $gender; | |
| return $this; | |
| } | |
| public function __toString() | |
| { | |
| return $this->name . ', ' . $this->gender; | |
| } | |
| } | |
| $person1 = new Person('Karol'); | |
| echo $person1 . PHP_EOL; | |
| $person2 = new Person('Anna'); | |
| echo $person2 . PHP_EOL; | |
| $person3 = new Person('Tomasz'); | |
| echo $person3 . PHP_EOL; | |
| $person4 = new Person('Kaja'); | |
| echo $person4 . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment