Skip to content

Instantly share code, notes, and snippets.

@kennyray
Created June 22, 2020 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennyray/2964ceebd3ee35426220beb3fcc93295 to your computer and use it in GitHub Desktop.
Save kennyray/2964ceebd3ee35426220beb3fcc93295 to your computer and use it in GitHub Desktop.
Classes
<?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();
@PiyarNET
Copy link

Thanks for the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment