1

Can someone explain me why i'm getting the results as below? (look at the comments).

class A {
    public function foo() {         // foo from B
    //private function foo() {      // foo from A
        echo "foo from A</br>";
    }
    public function test() {
        $this->foo();
    }
}

class B extends A {
    public function foo() {
        echo "foo from B</br>";
    }
}

$b = new B();
$b->test();

Shouldn't i always get "foo from B" while "this" pointing to class B object?

  • 1
    What results do you get? When I run the script here the output is `foo from B` as expected. – VolkerK Jul 01 '15 at 12:12
  • 1
    If the 'foo()` function was made available to sub classes via 'protected' or 'public' qualifiers then it works as you expect. However, it would be made clear in the interface or abstract class. In the previous case where `foo()` was `private` then it would not be mentioned anywhere available to you. – Ryan Vincent Jul 01 '15 at 15:15

1 Answers1

0

Because $this is using late binding method,

read official documentation about bindings (static or non-static does not matter)

http://php.net/manual/en/language.oop5.late-static-bindings.php