Understanding OOP principles when traversing objects / values

I am not very versed in two things in OOP, and I am going to use a fictitious understanding of SO to find out if I can help understand.

So, on this page we have a question. You can comment on the question. There are also answers. You can comment on the answers.

Question
 - comment
 - comment
 - comment

 Answer
  -comment

 Answer
  -comment
  -comment
  -comment

 Answer
  -comment
  -comment

So, I imagine a very high level of understanding of this type of system (in PHP, not .NET, since I am not familiar with .Net yet) it will look like this:

$question = new Question;
$question->load($this_question_id); // from the URL probably
echo $question->getTitle();

To download the answers, I imagine something like this ("A"):

$answers = new Answers;
$answers->loadFromQuestion($question->getID()); // or $answers->loadFromQuestion($this_question_id);
while($answer = $answers->getAnswer())
{
    echo $answer->showFormatted();
}

Or you would do ("B"):

$answers->setQuestion($question); // inject the whole obj, so we have access to all the data and public methods in $question
$answers->loadFromQuestion(); // the ID would be found via $this->question->getID() instead of from the argument passed in
while($answer = $answers->getAnswer())
{
    echo $answer->showFormatted();
}

, , , , . , , (, ). "" , ? .

, Hans

+3
5

, , , OO.

$question = new Question($id);
$comments = $question->getComments();
$answers = $question->getAnswers();

echo $question->getTitle();
echo $question->getText();

foreach ($comments as $comment)
    echo $comments->getText();

:

  • , , OO.
  • , , , .
  • . ( .)

( ) . OO.

$question = new Question($id);
$questionView = new QuestionView( $question );

$questionView->displayComments();
$questionView->displayAnswers();

.

, :

$question = new Question( $id );
$questionView = new QuestionView( $question );
$questionView->setPrinterFriendly();

$questionView->displayComments();
$questionView->displayAnswers();

, . (), - ; () - ( ).

, , . - , , , .

,

, :

Database -> Object -> Display Content

, , . , , , . , . , , - , .

, Comments; Questions Answers.

Comment :

  • ( )
  • ( )
  • .

CommentDB

CommentDB, , Comment . CommentDB :

  • Load
  • Update

, , , , , . , ( ).

:

  $commentDb = new CommentDB();
  $comment = $commentDb->create();

:

  $comment->update( "new text" );

, , , .

CommentView

, CommentView Comment. , Comment . - . Comment CommentView . , , , -.

, , , CommentView.

: " get/set" , ?

+4

? :

<?php
$question = new Question($id);
$comments = $question->getComments();
$answers = $question->getAnswers();

echo $question->getTitle();
echo $question->getText();

foreach ($comments as $comment)
    echo $comments->getText();

foreach ($answers as $answer)
{
    $answer_comments = $answer->getComments();
    echo $answer->getText();

    foreach ($answer_comments as $comment)
        echo $comment->getText();
}

getComments() getAnswers() $this->id ?

, . .

$question = new Question($id);
$answers = Answer::forQuestion($question->id);

$comments = Comment::forQuestion($question->id);
$ans_comments = Comment::forAnswer($answer->id);  // or some way to distinguish what the parent object is.

: , ( ) , id, db. . ( , PHP , 5.3.)

+4

. , . - , , , , , , .

, , , , . - , , , .

0

, @jasonbar:

, , .

Coupling, Cohesion, .

, .

PHP , . ( , , # Java, , C ++)

0

Dave Jarvis jasonbar, DataMappers ActiveRecord, , , :

:

  • QuestionMapper
  • AnswerMapper
  • CommentMapper

, :

  • save (object)// ( , )
  • ()
  • ()

:

$q = QuestionMapper::get( $questionid );

// here we could either (a) just return a list of Answers 
// previously eagerly-loaded by the
// QuestionMapper, or (b) lazy load the answers by 
// calling AnswerMapper::getByQuestionID( $this->id ) or similar.
$aAnswers = $q->getAnswers();
foreach($aAnswers as $oAnswer){
    echo $oAnswer->getText();

    $aComments = $oAnswer->getComments();
    foreach($aComments as $oComment){
        echo $oComment->getText();
    }
}

, QuestionView- > render ($ question), , . HTMLView, HTML; JSONView, JSON. , .

PS: We could also consider QuestionMapper to download everything related to questions, answers, and comments. Since comments always belong to answers or questions, and answers always belong to Questions, it may make sense that QuestionMapper has downloaded everything. Of course, we would need to consider various strategies for lazily loading a set of answers and comments to avoid server freezes.

0
source

Source: https://habr.com/ru/post/1781907/


All Articles