4

I was playing around with late static bindings in a non static context and noticed that the class in which a function is called is dependent on the visibility. Below I have added two scenario's where the only difference is the visibility of the foo() function in class A. This determines whether the output is A or B. Could someone explain me what the reasoning behind this or where I can find more documentation on this?

/**
* Scenario 1
**/
class A {
    private function foo() {
        echo "A\n";
    }
    public function test() {
        $this->foo();
    }
}

class B extends A {
    public function foo() { 
        echo "B\n";
    }
}

$b = new B;
$b->test(); //returns A


/**
* Scenario 2
**/
class A {
    public function foo() {
        echo "A\n";
    }
    public function test() {
        $this->foo();
    }
}

class B extends A {
    public function foo() {
        echo "B\n";
    }
}


$b = new B;
$b->test(); //returns B
Tom
  • 324
  • 4
  • 11
  • 2
    FYI, this has nothing to do with *late static binding*. There's no `static` involved here at all. – deceze May 21 '18 at 08:25
  • I'm sorry, I chose this definition as it was my starting point and I didn't really know how to call it otherwise. What would be a correct way to phrase this? I can edit my question to phrase it properly. – Tom May 21 '18 at 08:33
  • 1
    This [answer](https://stackoverflow.com/a/3756879/5356531) describes the behaviour best – Dmitry Nevzorov May 21 '18 at 08:35

0 Answers0