Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding inheritance in php

I'm new to using OOP in PHP (And in general) and I had a question about inheritance.

I have the following classes:

class OCITable {
  public function display() {
    $this->drawHeader();
    $this->drawFooter();
    $this->drawBody();
  }

  private function drawHeader() {
    ...
  }

  private function drawFooter() {
    ...
  }

  private function drawBody() {
    ...
  }
}

class OCITableServer extends OCITable {
  private function drawBody() {
    ...
  }
}

What I'm trying to do is overrule the private function drawBody(). This doesn't seem to work. I think this is because when a OCITableServer object calls display(), it calls the parent class's display(), which in turn calls its drawBody(), instead of the new drawBody().

How would I accomplish what I'm trying to do without redefining display() in my sub class?

like image 720
The_Denominater Avatar asked Feb 05 '26 22:02

The_Denominater


1 Answers

Protected methods can be overridden in subclasses. Private functions cannot.

like image 80
Bill Karwin Avatar answered Feb 07 '26 12:02

Bill Karwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!