Created
February 3, 2017 06:44
-
-
Save acapps/cf221c41038cd0a60a4e7774b61a5956 to your computer and use it in GitHub Desktop.
PHP Inheritance Example
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 | |
| class Supervisor { | |
| private $prop1; | |
| private $prop2; | |
| protected $key; | |
| protected function set($key, $val) { | |
| $this->$key = $val; | |
| } | |
| protected function get($key) { | |
| return $this->$key; | |
| } | |
| } | |
| class Child extends Supervisor { | |
| private $childProp1; | |
| private $childProp2; | |
| public function doStuff() { | |
| // work goes here | |
| } | |
| public function set($key, $val) { | |
| parent::set($key, $val); | |
| } | |
| public function get($key) { | |
| return parent::get($key); | |
| } | |
| } | |
| $slave = new Child(); | |
| $slave->set('asdf', 'Timmy'); | |
| echo $slave->get('asdf') . PHP_EOL; // Timmy gets written out. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment