Skip to content

Instantly share code, notes, and snippets.

@cesarkohl
Created August 6, 2019 16:21
Show Gist options
  • Select an option

  • Save cesarkohl/dbda1beb2b97df58b6f43cf9e8366c35 to your computer and use it in GitHub Desktop.

Select an option

Save cesarkohl/dbda1beb2b97df58b6f43cf9e8366c35 to your computer and use it in GitHub Desktop.
<?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