Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement