PHP中的可见性?继承时`$ this`的上下文?

lev*_*el0 1 php inheritance class-visibility

我是php新手,我正在浏览文档以获取可见性.我对文档中的这个例子感到困惑.当拨打电话时$myFoo->test(),不应该打电话给Foos $this->testPrivate();.我的意思是不$this应该Foo是对象而不是Bar对象?.根据我的知识(我可能在这里错了)Foo将有一些自己test()继承的方法,Bar并且调用$myFoo->test()将调用'$ this-> testPrivate',其中$this应该是Foos对象myFoo.那么它是如何调用BartestPrivate方法?

class Bar 
{
public function test() {
    $this->testPrivate();
    $this->testPublic();
}

public function testPublic() {
    echo "Bar::testPublic\n";
}

private function testPrivate() {
    echo "Bar::testPrivate\n";
}
}

class Foo extends Bar 
{
public function testPublic() {
    echo "Foo::testPublic\n";
}

private function testPrivate() {
    echo "Foo::testPrivate\n";
}
}

$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate 
            // Foo::testPublic
?>
Run Code Online (Sandbox Code Playgroud)

Mar*_*ker 5

test()Bar,并将调用Bar 有权访问的最高级别的方法.它可以访问Foo's testPublic(因为它public),所以它可以调用,但它没有访问Foo's testPrivate()(因为它是privateFoo),所以它调用它自己的testPrivate(),而不是