【发布时间】:2018-07-08 11:29:33
【问题描述】:
我有一个包含一些方法的父类(我不想编辑它)
class Message {
private function createNewMessageSimpleOrAlternativeBody() {
...some code here...
}
private function createNewMessageRelatedBody($oPart){
...some code here...
}
private function createNewMessageMixedBody($oPart){
...some code here...
}
public function ToPart($bWithoutBcc = false)
{
$oPart = $this->createNewMessageSimpleOrAlternativeBody();
$oPart = $this->createNewMessageRelatedBody($oPart);
$oPart = $this->createNewMessageMixedBody($oPart);
$oPart = $this->setDefaultHeaders($oPart, $bWithoutBcc);
return $oPart;
}
}
然后我有另一个在上面扩展的类,我想在其中添加我的代码
class MessageEx extends Message {
private function createNewMessageSimpleOrAlternativeBody() {
...some code here + additional code...
}
private function createNewMessageRelatedBody($oPart){
...some code here + additional code...
}
private function createNewMessageMixedBody($oPart){
...some code here + additional code...
}
public function ToPart($bWithoutBcc = false)
{
$oPart = $this->createNewMessageSimpleOrAlternativeBody();
$oPart = $this->createNewMessageRelatedBody($oPart);
$oPart = $this->createNewMessageMixedBody($oPart);
$oPart = $this->setDefaultHeaders($oPart, $bWithoutBcc);
return $oPart;
}
}
但是在扩展类中,当我尝试使用公共函数ToPart() 时,它告诉我在那里使用的任何方法都在父类中声明,而不是在这个子类中,我期望这是因为它们每个都是私有的并且 只能在 $this 的类中访问。在这种情况下,编辑的函数不会执行我的代码,因为即使我将它们(子类中的方法)声明为 final 或 protected
我错过了什么?
【问题讨论】:
-
请详细描述: 1. 你想做什么。 2. 添加您收到的确切错误消息
-
@ErikKalkoken 我已经更新了 OP。我的目标是不编辑父类,所以我创建了一个子类,在那里我完成了我的编辑,但是当调用我的扩展类时,它显示该类中的方法是在父类中声明的,而不是在那个子类中,所以我的代码没有被执行(没有错误)。
-
正如其他问题中提到的海报,它是关于继承如何在 php 中工作的。除非您编辑父类,否则您无法从子类 (AFAIK) 访问它们。
-
@Cemal 这就是我已经听说过的。只是想知道我是否在 PHP 代码中做错了什么。看起来我需要编辑父类。
标签: php oop extends overriding