我是 php 新手,我正在阅读文档以了解可见性。我对文档中的这个例子有点困惑。当调用 to 时$myFoo->test()
,不应该调用Foo
s $this->testPrivate();
。我的意思是不$this
应该Foo
是对象而不是Bar
对象?. 据我所知(我在这里可能错了)Foo
将有一种自己的test()
方法,该方法继承自Bar
并且调用$myFoo->test()
将调用 '$this->testPrivate' ,其中$this
应该是Foo
s object myFoo
。那么它是如何调用Bar
的testPrivate
方法呢?
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
?>