I have a parent class containing a function funcB() which I like to override with a better function by making just a few changes in this function. This function in the parent class makes a call to another private function i开发者_JS百科n the same class.
Sample code:
class classA {
private function funcA() {
return "funcA called";
}
public function funcB() {
$result = $this->funcA();
return $result;
}
}
class ClassB extends ClassA {
public function funcB($a) {
//do some more stuff
$result = $this->funcA();
return $result;
}
}
I get a Fatal error, because I'm not allowed to make a call to the private parent::funcA() function from within ClassB. But the call must being made. How is this still possible?
Declare the private
method as protected
instead.
See the documentation about visibility:
Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.
精彩评论