Skip to content

Instantly share code, notes, and snippets.

@cesarkohl
Created August 8, 2019 14:54
Show Gist options
  • Select an option

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

Select an option

Save cesarkohl/dac4f1326d3ca4362b648f67f4f81e62 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 Person {
protected $name = 'No Name';
private $birthYear = 'No Year';
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getBirthYear()
{
return $this->birthYear;
}
public function setBirthYear($birthYear)
{
$this->birthYear = $birthYear;
}
}
class Employee extends Person {
private $job;
public function setName($name)
{
// protected in Person()
$this->name = 'Employee: ' . $name;
}
public function setBirthYear($birthYear)
{
// private in Person()
$this->birthYear = 'Birth: ' . $birthYear;
}
}
$employee = new Employee();
$employee->setName('Name');
echo $employee->getName(); // return Employee: Name. As the property is PROTECTED
// the subclass CAN alter it.
$employee->setBirthYear('1900');
echo $employee->getBirthYear(); // return No Year. As the property is PRIVATE
// the subclass CANNOT alter it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment