Last active
December 20, 2015 22:09
-
-
Save mbrowne/6202957 to your computer and use it in GitHub Desktop.
PHP Simple OOP Example - Graduate and Undergraduate Students
This file contains 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 | |
class Student | |
{ | |
protected $name; | |
/** | |
* social security number | |
* @var string **/ | |
protected $ssn; | |
protected $major; | |
protected $gpa; | |
function __construct($name, $ssn, $major=null) { | |
$this->name = $name; | |
$this->ssn = $ssn; | |
$this->major = $major; | |
} | |
function getName() { | |
return $this->name; | |
} | |
function changeName($newName) { | |
$this->name = $newName; | |
} | |
function getSsn() { | |
return $this->ssn; | |
} | |
function getMajor() { | |
if ($this->major == null) { | |
return 'Undeclared'; | |
} | |
return $this->major; | |
} | |
function changeMajor($newMajor) { | |
$this->major = $newMajor; | |
} | |
function getGPA() { | |
//Throwing an exception here is optional; it depends on the application. | |
//We could alternatively consider it the programmer's responsibility to make sure | |
//the GPA is set before attempting to retrieve it (or test for null). | |
if ($this->gpa == null) { | |
throw new Exception("GPA was not set"); | |
} | |
return $this->gpa; | |
} | |
function setGPA($newGpa) { | |
$this->gpa = $newGpa; | |
} | |
} | |
class GraduateStudent extends Student | |
{ | |
const MASTERS = 1; | |
const PHD = 2; | |
private $degreeType; | |
function __construct($name, $ssn, $degreeType, $major=null) { | |
parent::__construct($name, $ssn, $major); | |
$this->setDegreeType($degreeType); | |
} | |
function getDegreeType() { | |
return $this->degreeType; | |
} | |
function setDegreeType($degree) { | |
if ($degree != self::MASTERS && $degree != self::PHD) { | |
throw new InvalidArgumentException("Degree must be a constant for either Masters or PhD"); | |
} | |
$this->degreeType = $degree; | |
} | |
} | |
class UndergraduateStudent extends Student | |
{ | |
private $whichYear; | |
private static $validYears = array( | |
'freshman', | |
'sophomore', | |
'junior', | |
'senior' | |
); | |
function __construct($name, $ssn, $whichYear, $major=null) { | |
parent::__construct($name, $ssn, $major); | |
$this->setYear($whichYear); | |
} | |
function getYear() { | |
return $this->whichYear; | |
} | |
function setYear($year) { | |
if (!in_array($year, self::$validYears)) { | |
throw new InvalidArgumentException("Invalid year '$year'"); | |
} | |
$this->whichYear = $year; | |
} | |
} | |
$fred = new GraduateStudent('Fred', '456-78-9012', GraduateStudent::MASTERS, 'Art'); | |
$fred->setGPA('3.7'); | |
$alice = new UndergraduateStudent('Alice', '123-45-6789', 'freshman'); | |
$alice->setGPA('4.0'); | |
echo "Fred's GPA is ". number_format($fred->getGPA(), 2)."\n"; | |
if ($fred->getDegreeType() == GraduateStudent::MASTERS) { | |
echo "Fred is pursuing a Masters in ".$fred->getMajor()."\n"; | |
} | |
echo "Alice's GPA is ". $alice->getGPA()."\n"; | |
echo "Alice's major is ". $alice->getMajor()."\n"; | |
echo "Alice was a ".$alice->getYear(); | |
$alice->setYear('sophomore'); | |
echo " but is now a ".$alice->getYear()."\n"; | |
class School | |
{ | |
protected $name; | |
protected $students = array(); | |
public function __construct($name) { | |
$this->name = $name; | |
} | |
public function changeName($newName) { | |
$this->name = $newName; | |
} | |
public function addStudent(Student $student) { | |
$this->students[$student->getSsn()] = $student; | |
} | |
public function getStudent($ssn) { | |
if (!isset($this->students[$ssn])) { | |
throw new Exception("Student with SSN '$ssn' not found in school '$this->name'!"); | |
} | |
return $this->students[$ssn]; | |
} | |
public function graduate(Student $student) { | |
$this->issueDiploma($student); | |
$this->removeStudent($student); | |
} | |
public function kickOut(Student $student) { | |
echo "Be gone with you!"; | |
$this->removeStudent($student); | |
} | |
protected function issueDiploma(Student $student) { | |
echo "Yay, ".$student->getName()." gets a diploma!"; | |
} | |
protected function removeStudent(Student $student) { | |
unset($this->students[$student->getSsn()]); | |
} | |
} | |
$RISD = new School('Rhode Island School of Design'); | |
$RISD->addStudent($fred); | |
$fredsSSN = $fred->getSsn(); | |
unset($fred); | |
//Later... | |
$fred = $RISD->getStudent($fredsSSN); | |
$RISD->graduate($fred); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment