开发者

Understanding inheritance in php

开发者 https://www.devze.com 2023-01-13 00:12 出处:网络
I\'m new to using OOP in PHP (And in general) and I had a question about inheritance. I have the following classes:

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 thin开发者_运维百科k 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?


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

0

精彩评论

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

关注公众号