views:

70

answers:

3

Consider the following piece of 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();

Now, changing the visibility of the m() method, I get:
(+ for public, - for 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()

(protected seems to behave like public).

I was expecting everything to behave like it does when both are declared public. But although foo->call() and bar->callbar() are essentially the same thing, they yield different results depending on the visibility of m() in foo and bar. Why does this happen?

+3  A: 

A private method is not overridable, as a private method is not visible even to its subclasses. Defining a method as protected means it is not visible outside of the class itself or its subclasses.

If you have a method that you want to use from your parent class but want children to able to modify its behaviour, and don't want this method available externally, use protected. If you want functionality in your parent class that cannot be modified in any way by subclasses, define the method as private.

EDIT: to clarify further, if you have two methods with the same name in a parent and subclass, and these methods are defined as private, essentially the subclass method has absolutely no relation to the parent method. As stated, a private method is COMPLETELY INVISIBLE to the subclass.

Consider this:

class foo {
    private function m() {
        echo 'foo->m() ';
    }
    private function z() { echo "foo->z();"; }

    public function call() {
        $this->m();
    }
}

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

Calling $bar->callz(); is going to produce an ERROR, because z does not exist in the subclass at all, not even as an inherited method.

Sam Day
"A private method is not overridable," It clearly is; if not, then why does `bar->callbar()` behave the way it does?
NullUserException
When `m()` in `foo` is `public`, then `bar` can override `m()`. Otherwise, `bar` overrides nothing; That's why only in the 4th case does `bar->call()` result with: `bar->m()`
SimpleCoder
@NullUserException: when you call bar->callbar() it's calling bar->m. Just because two methods have the same name doesn't mean one is being overridden (particularly when dealing with private methods).
Sam Day
@Simple Uh, not really. Even when `m()` in `foo` is `private`, `bar->callbar()` calls `m()` in `bar` (ugh, that's hard to read).
NullUserException
@NUE: Well, yes. That's what I said. `bar->callbar()` should call `m()` in `bar`; it can't override `m()` in `foo` because it is private.
SimpleCoder
@Simple , Sam See my edited question.
NullUserException
@NUE: I've tried to clarify further. Let me know if this still doesn't resolve your question.
Sam Day
-1 A private method is not completely invisible to its subclasses, it's actually copied to them. So they know they exist, they just cannot call it (the error message is even different).
Artefacto
+2  A: 

According to the PHP manual:

Members declared as private may only be accessed by the class that defines the member.

http://www.php.net/manual/en/language.oop5.visibility.php

EDIT

they yield different results depending on the visibility of m() in foo and bar. Why does this happen?

If m() in foo is public, it is overridable. When this is the case m() from bar overrides m() in foo.

SimpleCoder
+2  A: 

Inheriting/overriding private methods

In PHP, methods (including private ones) in the subclasses are either:

  • Copied; the scope of the original function is maintained.
  • Replaced ("overridden", if you want).

You can see this with this code:

<?php
class A {
    //calling B::h, because static:: resolves to B::
    function callH() { static::h(); }
    private function h() { echo "in A::h"; }
}
class B extends A {
    //not necessary; just to make explicit what's happening
    function callH() { parent::callH(); }
}
$b = new B;
$b->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:

<?php
class A {
    //calling B::h, because static:: resolves to B::
    function callH() { static::h(); }
    private function h() { echo "in A::h"; }
}
class B extends A {
    private function h() { echo "in B::h"; }
}
$b = new B;
$b->callH(); //fatal error; call to private method B::h() from context 'A'

Calling methods

Here the rules are as follows:

  • Look in the method table of the actual class of the object (in your case, bar).
    • If this yields a private method:
      • If the scope where the method was defined is the same as the scope of the calling function and is the same as the class of the object, use it.
      • Otherwise, look in the parent classes for a private method with the same scope as the one of the calling function and with the same name.
      • If no method is found that satisfies one of the above requirements, fail.
    • If this yields a public/protected method:
      • If the scope of the method is marked as having changed, we may have overridden a private method with a public/protected method. So in that case, and if, additionally, there's a method with the same name that is private as is defined for the scope of the calling function, use that instead.
      • Otherwise, use the found method.

Conclusion

  1. (Both private) For 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.
  2. (Private in 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.
  3. Nothing to see here, error because visibility was lowered.
  4. (Both public) The scope of 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.
Artefacto