Last active
September 28, 2022 18:23
-
-
Save rowleyaj/5817781 to your computer and use it in GitHub Desktop.
PHP OOP Example - Showing namespaces, abstract classes, visibility, and inheritance
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 OOP Example | |
=============== | |
Showing the use of classes and how they can be related using inheritance. | |
$person = new Person(); | |
Abstract classes can not be instantiated | |
Output: | |
Fatal Error | |
=========== | |
$teacher = new Teacher(); | |
$teacher->personalnumber = "1234"; | |
print($teacher->getPersonalNumber()); | |
Output: | |
13T1234 | |
======= | |
$student = new Student(); | |
$student->personalnumber = "9384"; | |
print($student->getPersonalNumber()); | |
Output: | |
13S9384 | |
======= | |
If the student is already expelled returns false, otherwise expelling | |
a student returns the $student so that more methods can be performed | |
on the student object: | |
e.g getParentDetails(), sendParentLetter() | |
$student = new Student(); | |
$expelledstudent = $student->expell(); | |
$expellexpelledstudent = $expelledstudent->expell(); | |
var_dump($expelledstudent); | |
var_dump($expellexpelledstudent); | |
Output: | |
object(rowleyaj\phpexample\Student)#1 (6) { | |
["personalnumberformat":protected]=> | |
string(5) "13S%s" | |
["expelled":"rowleyaj\phpexample\Student":private]=> | |
bool(true) | |
["name"]=> | |
NULL | |
["dateofbirth"]=> | |
NULL | |
["gender"]=> | |
NULL | |
["personalnumber"]=> | |
NULL | |
} | |
bool(false) |
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 | |
namespace rowleyaj\phpexample; | |
abstract class BaseModel | |
{ | |
// BaseModel created to allow shared methods to be added to | |
// objects without editing each individually | |
/** | |
* Prevent overloading of new properties | |
* @param string $name | |
* @return false | |
*/ | |
public function __get($name) | |
{ | |
return null; | |
} | |
/** | |
* Prevent overloading of new properties | |
* @param [type] $name [description] | |
* @param [type] $value [description] | |
*/ | |
public function __set($name, $value) | |
{ | |
return null; | |
} | |
} |
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 | |
namespace rowleyaj\phpexample; | |
abstract class Person extends BaseModel | |
{ | |
public $name; | |
public $dateofbirth; | |
public $gender; | |
public $personalnumber; | |
/** | |
* Format string for sprintf | |
* @var string | |
*/ | |
protected $personalnumberformat = '%s'; | |
/** | |
* Get formatted personal number ready for printing | |
* @return string | |
*/ | |
public function getPersonalNumber() | |
{ | |
// Assign vars only for readability | |
$number = $this->personalnumber; | |
$format = $this->personalnumberformat; | |
return sprintf($format, $number); | |
} | |
} |
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 | |
namespace rowleyaj\phpexample; | |
class Student extends Person | |
{ | |
/** | |
* Format string for sprintf | |
* @var string | |
*/ | |
protected $personalnumberformat = '13S%s'; | |
/** | |
* Represents if student is expelled. Can not be undone. | |
* @var bool | |
*/ | |
private $expelled = false; | |
/** | |
* Expell a student | |
* Expelling a student requires additional processing and callbacks | |
* so can not be set directly. e.g. Sending letter to parents | |
* | |
* If already expelled return false to prevent callbacks | |
* and additional processing | |
* @return Student or bool | |
*/ | |
public function expell() | |
{ | |
if ($this->expelled) { | |
return false; | |
} | |
$this->expelled = true; | |
return $this; // Allows chaining | |
} | |
} |
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 | |
namespace rowleyaj\phpexample; | |
class Teacher extends Person | |
{ | |
/** | |
* Format string for sprintf | |
* @var string | |
*/ | |
protected $personalnumberformat = '13T%s'; | |
/** | |
* Line Manager - Teacher or false | |
* @var Teacher or Bool | |
*/ | |
public $linemanager = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment