Skip to content

Instantly share code, notes, and snippets.

@jonathas
Created September 2, 2021 10:58
Show Gist options
  • Select an option

  • Save jonathas/f015540b6b521572e19e623ae8bffea5 to your computer and use it in GitHub Desktop.

Select an option

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
<?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