Consider the following piece of code:
class foo {
private function m() {
echo \'foo->m() \';
}
public function call() {
$this-
In PHP, methods (including private ones) in the subclasses are either:
You can see this with this code:
callH();
Now if you override the private method, its new scope will not be A, it will be B, and the call will fail because A::callH()
runs in scope A
:
callH(); //fatal error; call to private method B::h() from context 'A'
Here the rules are as follows:
bar
).
bar->call()
, the scope of call
is foo
. Calling $this->m()
elicits a lookup in the method table of bar
for m
, yielding a private bar::m()
. However, the scope of bar::m()
is different from the calling scope, which foo
. The method foo:m()
is found when traversing up the hierarchy and is used instead.foo
, public in bar
) The scope of call
is still foo
. The lookup yields a public bar::m()
. However, its scope is marked as having changed, so a lookup is made in the function table of the calling scope foo
for method m()
. This yields a private method foo:m()
with the same scope as the calling scope, so it's used instead.call
is still foo
. The lookup yields a public bar::m()
. Its scope isn't marked as having changed (they're both public), so bar::m()
is used.