Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7. class Car {
  8.  
  9. protected $color = 'White';
  10.  
  11. public function setColor($color)
  12. {
  13. $this->color = $color;
  14. }
  15.  
  16. public function getColor()
  17. {
  18. return $this->color;
  19. }
  20.  
  21. }
  22.  
  23. $car = new Car();
  24. echo $car->color; // Return Fatal Error: Cannot access protected property
  25. echo $car->getColor(); // Return White. The function getColor() is public.
  26.  
  27. $car->setColor('Yellow');
  28. echo $car->getColor(); // Return Yellow. The function getColor() is public.
  29.  
  30. ////////////////////////////////////////////////////////////////
  31.  
  32. class Beetle extends Car {
  33.  
  34.  
  35.  
  36. }
  37.  
  38. $beetle = new Beetle();
  39. echo $beetle->color; // Return Fatal Error: Cannot access protected property
  40. echo $beetle->getColor(); // Return White. The function getColor() is public.
  41.  
  42. $beetle->setColor('Green');
  43. echo $beetle->getColor(); // Return Green. The function getColor() is public.
  44.  
  45. ////////////////////////////////////////////////////////////////
  46.  
  47. class Ferrari extends Car {
  48.  
  49. protected $color = 'RedFerrari';
  50.  
  51. }
  52.  
  53. $ferrari = new Ferrari();
  54. echo $ferrari->color; // Return Fatal Error: Cannot access protected property
  55. echo $ferrari->getColor(); // Return RedFerrari. The function getColor() is public.
  56.  
  57. $ferrari->setColor('Red');
  58. echo $ferrari->getColor(); // return Red
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement