【发布时间】:2021-12-07 15:11:03
【问题描述】:
我不明白为什么要打印 // Bar::testPrivate 和 // Foo::testPublic .... "$this" 有优先权吗?我了解 Foo::testPublic 但 Bar::testPrivate 不了解 enter image description here
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
?>
【问题讨论】:
-
你看到
private是什么意思了吗? php.net/manual/en/language.oop5.visibility.php -
当然,但是 Foo 类应该调用它自己的 testPrivate() 方法而不是 Bar 的 testPrivate() 方法吗?