【发布时间】:2010-12-23 20:10:52
【问题描述】:
我对 OOP 中的一些事情不太了解,我将使用对 SO 的虚构理解来看看我是否可以得到帮助理解。
所以,在这个页面上我们有一个问题。您可以对问题发表评论。也有答案。您可以对答案发表评论。
Question
- comment
- comment
- comment
Answer
-comment
Answer
-comment
-comment
-comment
Answer
-comment
-comment
所以,我想象对这种类型的系统(在 PHP 中,而不是 .Net 中,因为我还不熟悉 .Net)有一个非常高层次的理解:
$question = new Question;
$question->load($this_question_id); // from the URL probably
echo $question->getTitle();
要加载答案,我想它是这样的(“A”):
$answers = new Answers;
$answers->loadFromQuestion($question->getID()); // or $answers->loadFromQuestion($this_question_id);
while($answer = $answers->getAnswer())
{
echo $answer->showFormatted();
}
或者,你会做(“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();
}
我想我的问题是,我不知道何时或是否应该传递整个对象,以及何时应该只传递一个值。传入整个对象给了我很大的灵活性,但我猜它更多的内存并且可能会发生变化(比如属性或方法重命名)。如果“A”样式更好,为什么不直接使用函数呢? OOP 在这里似乎毫无意义。
谢谢, 汉斯
【问题讨论】: