Created
June 22, 2020 20:18
-
-
Save kennyray/2964ceebd3ee35426220beb3fcc93295 to your computer and use it in GitHub Desktop.
Classes
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 | |
abstract class LifeForm | |
{ | |
protected $firstName; | |
protected $middleName; | |
protected $lastName; | |
public function __construct(string $firstName, string $lastName= "", string $middleName = "") | |
{ | |
$this->firstName = $firstName; | |
$this->middleName = $middleName; | |
$this->lastName = $lastName; | |
} | |
public function getFirstName(): string | |
{ | |
return $this->firstName; | |
} | |
public function getMiddleName(): string | |
{ | |
return $this->middleName; | |
} | |
public function getLastName(): string | |
{ | |
return $this->lastName; | |
} | |
} | |
class Person extends LifeForm | |
{ | |
public function __construct(string $firstName, string $lastName, string $middleName = "") | |
{ | |
parent::__construct($firstName, $lastName, $middleName); | |
} | |
} | |
class Employee extends Person | |
{ | |
protected $id; | |
public function __construct(int $id, string $firstName, string $lastName, string $middleName = "") | |
{ | |
$this->id = $id; | |
parent::__construct($firstName, $lastName, $middleName); | |
} | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
} | |
class Pet extends LifeForm | |
{ | |
public function __construct(string $firstName, string $lastName="", string $middleName = "") | |
{ | |
parent::__construct($firstName, $lastName, $middleName); | |
} | |
} | |
// Quick and dirty tests to ensure it works properly | |
$pet1 = new Pet('Fluffy', 'Nutter', 'Mac'); | |
echo $pet1->getFirstName() . ' ' . $pet1->getMiddleName() . ' ' . $pet1->getLastName(); | |
echo PHP_EOL; | |
$pet2 = new Pet('Zinga', 'Ray'); | |
echo $pet2->getFirstName() . ' ' . $pet2->getMiddleName() . ' ' . $pet2->getLastName(); | |
echo PHP_EOL; | |
$pet3 = new Pet('Spot'); | |
echo $pet3->getFirstName() . ' ' . $pet3->getMiddleName() . ' ' . $pet3->getLastName(); | |
echo PHP_EOL; | |
$person1 = new Person('Joe', 'Smith'); | |
echo $person1->getFirstName() . ' ' . $person1->getLastName(); | |
echo PHP_EOL; | |
$person2 = new Person('Martha', 'Redhouse', 'Lee'); | |
echo $person2->getFirstName() . ' ' . $person2->getMiddleName() . ' ' . $person2->getLastName(); | |
echo PHP_EOL; | |
$employee1 = new Employee(362, 'Bob', 'Williams', 'Howard'); | |
echo "({$employee1->getId()}) " . $employee1->getFirstName() . ' ' . $employee1->getMiddleName() . ' ' . $employee1->getLastName(); | |
echo PHP_EOL; | |
$employee2 = new Employee(496, 'Lucy', 'Lou'); | |
echo "({$employee2->getId()}) " . $employee2->getFirstName() . ' ' . $employee2->getLastName(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example.