Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While I like Jason's answer, it is not, strictly speaking OO.</p> <pre><code>$question = new Question($id); $comments = $question-&gt;getComments(); $answers = $question-&gt;getAnswers(); echo $question-&gt;getTitle(); echo $question-&gt;getText(); foreach ($comments as $comment) echo $comments-&gt;getText(); </code></pre> <p>The problems are:</p> <ol> <li>There is no information hiding, a fundamental principle of OO.</li> <li>If the format of the answers needs to change, it must be changed in a place that is not associated with the object that houses the data.</li> <li>The solution is not extensible. (There is no behaviour to inherit.)</li> </ol> <p>You <em>must</em> keep behaviour (tightly coupled) with the data. Otherwise you are not writing OO.</p> <pre><code>$question = new Question($id); $questionView = new QuestionView( $question ); $questionView-&gt;displayComments(); $questionView-&gt;displayAnswers(); </code></pre> <p>How the information is displayed is now an implementation detail, and reusable.</p> <p>Notice how this opens up the following possibility:</p> <pre><code>$question = new Question( $id ); $questionView = new QuestionView( $question ); $questionView-&gt;setPrinterFriendly(); $questionView-&gt;displayComments(); $questionView-&gt;displayAnswers(); </code></pre> <p>The idea is that now you can change <em>how</em> the questions are formatted from a single location in the code base. You can support multiple formats for the comments and answers without the calling code (a) ever knowing; and (b) ever needing to change (to a significant degree).</p> <p>If you are coding text formatting details in more than one location because you are misusing accessor methods, the life of any future maintainers will be miserable. If the maintainer is a psychopath who knows where you live, you will be in trouble.</p> <h3>Objects, Data, and Views</h3> <p>Here's the problem, as I understand it:</p> <pre><code>Database -&gt; Object -&gt; Display Content </code></pre> <p>You want to keep the behaviour of the object centred around logic that is intrinsic to the object. In other words, you don't want the Object to have to do things that have nothing to do with its core responsibilities. Most commonly this will include load, save, and print functionality. You want to keep these separate from the object itself because if you ever have to change database, or output format, you want to make as few changes in the system as possible, and restrain the ripple effect.</p> <p>To simplify this, let's take a look at loading only <code>Comments</code>; everything is applicable to <code>Questions</code> and <code>Answers</code> as well.</p> <p><strong>Comment Class</strong></p> <p>The Comment class might offer the following behaviours:</p> <ul> <li>Reply</li> <li>Delete</li> <li>Update (requires permission)</li> <li>Restore (from a delete)</li> <li>etc.</li> </ul> <p><strong>CommentDB Class</strong></p> <p>We can create a <code>CommentDB</code> object that knows how to manipulate the <code>Comment</code>s in the database. A <code>CommentDB</code> object has the following behaviours:</p> <ul> <li>Create</li> <li>Load</li> <li>Save</li> <li>Update</li> <li>Delete</li> <li>Restore</li> </ul> <p>Notice that these behaviours will likely be common across all objects and can therefore be subject to refactoring. This will also let you change databases quite easily as the connection information will be isolated to a single class (the grandfather of all database objects).</p> <p>Example usage:</p> <pre><code> $commentDb = new CommentDB(); $comment = $commentDb-&gt;create(); </code></pre> <p>Later:</p> <pre><code> $comment-&gt;update( "new text" ); </code></pre> <p>Notice that there are a number of possible ways to implement this, but you can always do so without violating encapsulation and information hiding.</p> <p><strong>CommentView Class</strong></p> <p>Lastly, the <code>CommentView</code> class will be tightly coupled to a <code>Comment</code> class. That it can obtain the attributes of <code>Comment</code> class via accessors is expected. The information is still hidden <em>from the rest of the system</em>. The <code>Comment</code> and its <code>CommentView</code> are tightly coupled. The idea is that the formatting is kept in a single place, not scattered throughout classes that need to use the data willy nilly.</p> <p>Any classes that need to display comments, but in a slightly different format, can inherit from <code>CommentView</code>.</p> <p>See also: <a href="https://stackoverflow.com/questions/996179/allen-holub-wrote-you-should-never-use-get-set-functions-is-he-correct">Allen Holub wrote &quot;You should never use get/set functions&quot;, is he correct?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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