相关疑难解决方法(0)

覆盖私有方法时的奇怪行为

考虑以下代码:

class foo {
    private function m() {
        echo 'foo->m() ';
    }
    public function call() {
        $this->m();
    }
}

class bar extends foo {
    private function m() {
        echo 'bar->m() ';
    }
    public function callbar() {
        $this->m();
    }
}

$bar = new bar;

$bar->call();
$bar->callbar();
Run Code Online (Sandbox Code Playgroud)

现在,改变的可见性m()的方法,我得到:
(+public,-private)

Visibility              bar->call()    bar->callbar() 
======================================================
-foo->m(), -bar->m()    foo->m()       bar->m()
-foo->m(), +bar->m()    foo->m()       bar->m()
+foo->m(), -bar->m()    ERROR          ERROR
+foo->m(), +bar->m()    bar->m()       bar->m()
Run Code Online (Sandbox Code Playgroud)

(protected似乎表现得像 …

php overriding visibility private

18
推荐指数
2
解决办法
1万
查看次数

标签 统计

overriding ×1

php ×1

private ×1

visibility ×1