Answer the question
In order to leave comments, you need to log in
Why is the public method not inherited in this PHP code?
I understand OOP, I came across such a code, it seems simple, but I can’t understand
<?php
abstract class base {
public function inherited() {
$this->overridden();
}
private function overridden() {
echo 'base';
}
}
class child extends base {
private function overridden() {
echo 'child';
}
}
$test = new child();
$test->inherited();
?>
Output will be "base".
Answer the question
In order to leave comments, you need to log in
Inherited does not mean "copied". It's just that when calling $test->inherited() in the child class, the function will not be found and there will be an attempt to call the function from the parent class base.
Well, the base class, as Lentyuy already wrote , does not see overriden() from child, since it is private.
inherited.
and not the method that you expect is called, because in the context of base nothing is known about child:: overridden (), because it is private. if you remove the base::overridden() declaration, there will be an error.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question