【发布时间】:2012-09-29 10:41:27
【问题描述】:
我对 php 手册中的示例感到困惑。这是关于能见度的。这是一个例子。
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();
?>
http://www.php.net/manual/en/language.oop5.visibility.php
这个例子输出
Bar::testPrivate
Foo::testPublic
请您解释一下这是怎么发生的?
为什么两个testPublic() 都没有被调用?
我在 Bar 类构造中放置了一个 var_dump($this)。它打印object(Foo)[1]。我知道的是私有属性可以在同一个类中调用。
那么“Bar::testPrivate”是怎么调用的呢?
【问题讨论】:
标签: php visibility