Created
August 8, 2019 14:54
-
-
Save cesarkohl/dac4f1326d3ca4362b648f67f4f81e62 to your computer and use it in GitHub Desktop.
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 | |
| ini_set('display_errors', 1); | |
| ini_set('display_startup_errors', 1); | |
| error_reporting(E_ALL); | |
| class Person { | |
| protected $name = 'No Name'; | |
| private $birthYear = 'No Year'; | |
| public function getName() | |
| { | |
| return $this->name; | |
| } | |
| public function setName($name) | |
| { | |
| $this->name = $name; | |
| } | |
| public function getBirthYear() | |
| { | |
| return $this->birthYear; | |
| } | |
| public function setBirthYear($birthYear) | |
| { | |
| $this->birthYear = $birthYear; | |
| } | |
| } | |
| class Employee extends Person { | |
| private $job; | |
| public function setName($name) | |
| { | |
| // protected in Person() | |
| $this->name = 'Employee: ' . $name; | |
| } | |
| public function setBirthYear($birthYear) | |
| { | |
| // private in Person() | |
| $this->birthYear = 'Birth: ' . $birthYear; | |
| } | |
| } | |
| $employee = new Employee(); | |
| $employee->setName('Name'); | |
| echo $employee->getName(); // return Employee: Name. As the property is PROTECTED | |
| // the subclass CAN alter it. | |
| $employee->setBirthYear('1900'); | |
| echo $employee->getBirthYear(); // return No Year. As the property is PRIVATE | |
| // the subclass CANNOT alter it. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment