-1

I have a parent class (I don't want to edit it) which contains some methods

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;
    }
}

then I have another class which extends above and where I want to add my code

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;
    }
}

but in an extended class when I try to use public function ToPart() it tells me that any method used there was declared in a parent class not in this subclass, which I have expected as each of them is private and can be accessed only within the class vis $this. In that case edited functions are not executing my code as those are not called even when I have declared them (methods in a subclass) as final or protected

What I have missed?

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • 1
    Please describe in more detail: 1. What you want to do. 2. Add the exact error message you get – Erik Kalkoken Jan 29 '18 at 17:36
  • @ErikKalkoken I have updated OP. My goal is to do not edit parent class so I have created a subclass where I have done my edits but when calling my extended class it shows that methods within that class were declared in a parent class not that subclass so my code was not executed (no errors). – JackTheKnife Jan 29 '18 at 18:20
  • Possible duplicate of [Call private method from inherited class](https://stackoverflow.com/questions/12603766/call-private-method-from-inherited-class) – Cemal Jan 29 '18 at 18:22
  • @Cemal Is not as I'm trying to access private method within the class. – JackTheKnife Jan 29 '18 at 18:29
  • Have you read https://stackoverflow.com/a/12603807/4065790 . It tells you what to do about this – Cemal Jan 29 '18 at 18:32
  • @Cemal I have tried to switch methods in a subclass to `final` or `protected` - nothing works. Each time it says that all methods were declared in the parent and my code was not executed. – JackTheKnife Jan 29 '18 at 18:36
  • 1
    As the poster in other questio mentioned, it's about how inheritance works in php. There's nothing you can do from subclass (AFAIK), to access them unless you edit the parent class. – Cemal Jan 29 '18 at 18:40
  • 1
    @Cemal that is what I have heard already. Just wondering if I have done something wrong or not in PHP code. Looks like I will need to edit parent class then. – JackTheKnife Jan 29 '18 at 18:42

1 Answers1

-1

If I understand you correctly you want to add code to the existing methods from Message by trying to override them in your inherited class MessageEx.

Although your code will technically work as posted, your methods in MessageEx (e.g. createNewMessageSimpleOrAlternativeBody() ) will not be able to call their pendants in Message, because the methods in Message are private. Using final or protected in the subclass will not work.

But what you can do is call the public method ToPart from Message with parent::ToPart(..), which in turn will call the private methods in Message.

Or if that does not fit to the logic of your code, your only other option is to change the methods in Message to protected, so that they become accessible in MessageEx.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114