Hello,
I starting learning OOP, and I love it! I was playing around with access modifiers and came across something I had difficulty understanding.
On The code below i set the function protected1() to be protected. Which is supposed to be accessed from the class or subclass
so I created a subclass called example2 that extends example. Then i set $example1 to instantiated sub class example2 and called protected1() function, and it did not call work?
Why is that?
class Example {
public function public1() { // Everywhere
echo "This is Public";
}
private function private1() { // This class only
echo "This is private";
}
protected function protected1() { // this and the subclasses only
echo "This is protected";
}
}
class example2 extends example {
}
$example1 = new example2();
echo $example1->protected1();