class Person
{
public function pub()
{
echo 'I am Publicly accessible<br>';
}
private function pri()
{
echo 'I am Private function<br>';
}
protected function pro()
{
echo 'I am Protected function<br>';
}
}
class NewPerson extends Person
{
public function getPrivatedata()
{
parent::pri();
}
public function getProtectedata()
{
parent::pro();
}
}
$newperson = new NewPerson;
//$newperson->getPrivatedata(); // Fatal error: Call to private method Person::pri() from context 'NewPerson'
$newperson->getProtectedata(); // Output is I am Protected function
from the above example newperson object trying to access a function , that is defined as private from its class Person. that shows error message. because we cant access it from derived class.after that we call a function getProtectedata() . it access protected function pro from Person. It can be accessible , because of protected.
so whenever we want to access a function from its child class, it should be defined as protected, else private.
No comments:
Post a Comment