It may sound silly, but I'm new in PHP. I was learning from the documentation about access specifiers when I came to this section.
class Bar {
public function __construct() {
echo "Bar::constructor<br />";
}
public function test() {
$this->PublicTest();
$this->PrivateTest();
$this->protectedTest();
}
public function PublicTest(){
echo "Bar::testPublic<br />";
}
private function PrivateTest() {
echo "Bar::testPrivate<br />";
}
protected function ProtectedTest() {
echo "Bar::testProtected<br />";
}
}
class Foo extends Bar {
public function __construct() {
echo "Foo::constructor<br />";
}
public function PublicTest() {
echo "Foo::testPublic<br />";
}
private function PrivateTest() {
echo "Foo::testPrivate<br />";
}
protected function ProtectedTest() {
echo "Foo::testProtected<br />";
}
}
$myFoo = new Foo();
$myFoo->test();
?>
This produces output as:
Foo::constructor
Foo::testPublic
Bar::testPrivate
Foo::testProtected
Why does it prints from Bar class for private function while it prints from Foo class for public and protected function? Since, i don't have test() function in Foo class, it accesses the test() function from Bar class.
Where does $this pointer point to? Does it point to the function of Foo class or functions of Bar class? I'm really confused here. Can someone please explain this to me? Any help would be so much appreciated.