开发者

Making a private call in a extended class?

开发者 https://www.devze.com 2023-02-18 06:27 出处:网络
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 ano

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号