Created
August 6, 2019 16:21
-
-
Save cesarkohl/dbda1beb2b97df58b6f43cf9e8366c35 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 Car { | |
| protected $color = 'White'; | |
| public function setColor($color) | |
| { | |
| $this->color = $color; | |
| } | |
| public function getColor() | |
| { | |
| return $this->color; | |
| } | |
| } | |
| $car = new Car(); | |
| echo $car->color; // Return Fatal Error: Cannot access protected property | |
| echo $car->getColor(); // Return White. The function getColor() is public. | |
| $car->setColor('Yellow'); | |
| echo $car->getColor(); // Return Yellow. The function getColor() is public. | |
| //////////////////////////////////////////////////////////////// | |
| class Beetle extends Car { | |
| } | |
| $beetle = new Beetle(); | |
| echo $beetle->color; // Return Fatal Error: Cannot access protected property | |
| echo $beetle->getColor(); // Return White. The function getColor() is public. | |
| $beetle->setColor('Green'); | |
| echo $beetle->getColor(); // Return Green. The function getColor() is public. | |
| //////////////////////////////////////////////////////////////// | |
| class Ferrari extends Car { | |
| protected $color = 'RedFerrari'; | |
| } | |
| $ferrari = new Ferrari(); | |
| echo $ferrari->color; // Return Fatal Error: Cannot access protected property | |
| echo $ferrari->getColor(); // Return RedFerrari. The function getColor() is public. | |
| $ferrari->setColor('Red'); | |
| echo $ferrari->getColor(); // return Red | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment