Created
September 2, 2021 10:58
-
-
Save jonathas/f015540b6b521572e19e623ae8bffea5 to your computer and use it in GitHub Desktop.
Simplify/fix the code below by knowing that you can use php 7.3-7.4
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 | |
| /** | |
| * Simplify/fix the code below by knowing that you can use php 7.3-7.4: | |
| */ | |
| class User { | |
| public $name; | |
| public $lastName; | |
| protected $age; | |
| public function __construct($name, $lastName, $age) | |
| { | |
| if (is_string($name)) { | |
| $this->name = $name; | |
| } else { | |
| throw new \Exception('Name is not a string'); | |
| } | |
| if (is_string($name)) { | |
| $this->lastName = $lastName; | |
| } else { | |
| throw new \Exception('Last name is not a string'); | |
| } | |
| if (is_int($age)) { | |
| $this->age = $age; | |
| } else { | |
| throw new \Exception('Age is not an integer'); | |
| } | |
| } | |
| function printUserName(): string | |
| { | |
| echo $user->name,' ', $user->lastName; | |
| } | |
| } | |
| $user = new User('Matt', 'Jefferson', 34); | |
| if ($user->age > 32) { | |
| $user->printUserName(); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment