請看下面給出的代碼。php5 ---方法可見性問題
<?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(); // Bar::testPrivate
// Foo::testPublic
?>
在上面的例子中,當我們呼籲$myFoo->test();
它所謂的Bar class
testPrivate
但怎麼就叫做Foo
類testPublic
。
任何人都可以幫助我嗎?
那麼爲什麼它可以訪問Foo的testPublic(),因爲testPublic of foo不在Bar範圍內? –
PHP嘗試訪問儘可能。 Foo只是擴展Bar :: test()而不是複製它!所以test()運行在它的所有者範圍內(Bar),但是當PHP試圖儘可能地訪問方法時,它使用Foo :: testPublic(),它存在且可訪問FOR Bar,但testPrivate不可訪問FOR Foo ... – AHHP