Strange behavior when overriding private methods

ragiz

New Member
Consider the following piece of code:\[code\]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();\[/code\]Now, changing the visibility of the \[code\]m()\[/code\] method, I get:
(\[code\]+\[/code\] for \[code\]public\[/code\], \[code\]-\[/code\] for \[code\]private\[/code\])\[code\]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()\[/code\](\[code\]protected\[/code\] seems to behave like \[code\]public\[/code\]).I was expecting everything to behave like it does when both are declared \[code\]public\[/code\]. But although \[code\]foo->call()\[/code\] and \[code\]bar->callbar()\[/code\] are essentially the same thing, they yield different results depending on the visibility of \[code\]m()\[/code\] in \[code\]foo\[/code\] and \[code\]bar\[/code\]. Why does this happen?
 
Back
Top