I don’t understand how this example from PHP documentation works :
https://www.php.net/manual/en/language.oop5.late-static-bindings.php
I quote from the doc “In non-static contexts, the called class will be the class of the object instance” means if i add an echo get_class($this) in method test it will display B then C, the first one is then a foo from B (foo is private in A so there is no foo in B), and the second one is a foo from C, and this means it should call the private empty foo from C ? but that’s not it!
<?php
class A {
private function foo() {
echo "success!n";
}
public function test() {
$this->foo();
static::foo();
}
}
class B extends A {
/* foo() will be copied to B, hence its scope will still be A and
* the call be successful */
}
class C extends A {
private function foo() {
/* original method is replaced; the scope of the new one is C */
}
}
$b = new B();
$b->test();
$c = new C();
$c->test(); //fails
?>
Output :
success!
success!
success!
Fatal error: Call to private method C::foo() from context 'A' in /tmp/test.php on line 9