Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a private call in a extended class?

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 in 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?

like image 811
netiul Avatar asked Dec 21 '22 16:12

netiul


1 Answers

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.

like image 184
Felix Kling Avatar answered Dec 24 '22 06:12

Felix Kling