Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Alex, most of the times you need multiple inheritance is a signal your object structure is somewhat incorrect. In situation you outlined I see you have class responsibility simply too broad. If Message is part of application business model, it should not take care about rendering output. Instead, you could split responsibility and use MessageDispatcher that sends the Message passed using text or html backend. I don't know your code, but let me simulate it this way:</p> <pre><code>$m = new Message(); $m-&gt;type = 'text/html'; $m-&gt;from = 'John Doe &lt;jdoe@yahoo.com&gt;'; $m-&gt;to = 'Random Hacker &lt;rh@gmail.com&gt;'; $m-&gt;subject = 'Invitation email'; $m-&gt;importBody('invitation.html'); $d = new MessageDispatcher(); $d-&gt;dispatch($m); </code></pre> <p>This way you can add some specialisation to Message class:</p> <pre><code>$htmlIM = new InvitationHTMLMessage(); // html type, subject and body configuration in constructor $textIM = new InvitationTextMessage(); // text type, subject and body configuration in constructor $d = new MessageDispatcher(); $d-&gt;dispatch($htmlIM); $d-&gt;dispatch($textIM); </code></pre> <p>Note that MessageDispatcher would make a decision whether to send as HTML or plain text depending on <code>type</code> property in Message object passed.</p> <pre><code>// in MessageDispatcher class public function dispatch(Message $m) { if ($m-&gt;type == 'text/plain') { $this-&gt;sendAsText($m); } elseif ($m-&gt;type == 'text/html') { $this-&gt;sendAsHTML($m); } else { throw new Exception("MIME type {$m-&gt;type} not supported"); } } </code></pre> <p>To sum it up, responsibility is split between two classes. Message configuration is done in InvitationHTMLMessage/InvitationTextMessage class, and sending algorithm is delegated to dispatcher. This is called Strategy Pattern, you can read more on it <a href="http://www.dofactory.com/Patterns/PatternStrategy.aspx" rel="noreferrer">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload