<?PHP class A { private function foo() { echo "baf!<br/>"; } public function test() { $this->foo(); } } class C extends A { private function foo() { echo "bar!<br/>"; } } $c = new C(); $c->test(); //Prints baf! ?>
如何使c-> test()打印吧! ?我期待foo()将在C中被覆盖,并打印吧!有人可以向我解释吗?
通过使这两种方法受到保护,您将获得所需的结果;原因是在A :: test()中,由于私有的可见性,它只能将$this-> foo()解析为A :: foo().见
Visibility.
class A { protected function foo() { echo "baf!<br/>"; } public function test() { $this->foo(); } } class C extends A { protected function foo() { echo "bar!<br/>"; } } $c = new C(); $c->test(); //Prints bar!