了解php中的继承(Understanding inheritance in php)

我是新手在PHP中使用OOP(一般而言),我有一个关于继承的问题。

我有以下课程:

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() { ... } }

我想要做的是否决私有函数drawBody() 。 这似乎不起作用。 我认为这是因为当OCITableServer对象调用display() ,它调用父类的display() ,而后者又调用drawBody()而不是new drawBody() 。

如果不在子类中重新定义display() ,我将如何完成我想要做的事情?

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?

最满意答案

Protected方法可以在子类中重写。 私人职能不能。

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

更多推荐