Skip to content

Instantly share code, notes, and snippets.

@adduc
Created May 18, 2015 14:43
Show Gist options
  • Save adduc/defc23ce9040aff5d0e5 to your computer and use it in GitHub Desktop.
Save adduc/defc23ce9040aff5d0e5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
class Person
{
public $first_name;
public $last_name;
public function getFullName()
{
return $this->first_name . " " . $this->last_name;
}
}
class Employee extends Person
{
public $title;
public static function printTitle(Employee $employee)
{
echo $employee->title;
}
}
class Intern extends Person
{
const TITLE = "Intern";
public static function printTitle()
{
echo static::TITLE;
}
}
class Supervisor extends Employee
{
protected $employees = array();
public function getEmployees()
{
return $this->employees;
}
public function addEmployee(Employee $employee)
{
foreach (array_keys($this->employees) as $index) {
if ($this->employees[$index] == $employee) {
return false;
}
}
$this->employees[] = $employee;
}
public function removeEmployee(Employee $employee)
{
foreach (array_keys($this->employees) as $index) {
if ($employee == $this->employees[$index]) {
unset($this->employees[$index]);
}
}
}
}
$person = new Person();
$person->first_name = "First";
$person->last_name = "Last";
echo $person->getFullName() . "\n";
$employee = new Employee();
$employee->first_name = "First";
$employee->last_name = "Last";
echo $employee->getFullName() . "\n";
$employee->title = "Senior Executive Vice President";
$employee::printTitle($employee);
echo "\n";
$intern = new Intern();
$intern->first_name = "First";
$intern->last_name = "Last";
echo $intern->getFullName() . "\n";
$intern::printTitle($intern);
echo "\n";
$supervisor = new Supervisor();
$intern->first_name = "First";
$intern->last_name = "Last";
$supervisor->addEmployee($employee);
// $supervisor->addEmployee($intern);
var_dump($supervisor->getEmployees());
$supervisor->removeEmployee($employee);
var_dump($supervisor->getEmployees());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment