Skip to content

Instantly share code, notes, and snippets.

@cesarkohl
Last active August 6, 2019 15:54
Show Gist options
  • Select an option

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

Select an option

Save cesarkohl/89c9e9d71c3bea803da08a4765e9197c 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 {
private $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 private 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; // Notice: Undefined property. The parent property keeps in it.
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 {
private $color = 'RedFerrari';
// public function getColor()
// {
// return $this->color;
// }
}
$ferrari = new Ferrari();
echo $ferrari->color; // Return Fatal Error: Cannot access private property
echo $ferrari->getColor(); // Return White. 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