【发布时间】:2010-06-12 11:31:04
【问题描述】:
我有一个对象在方法中加载另一个对象的实例。
$survey = new survey;
$question = new question($survey);
$question->load($question_id);
class question {
public function __construct(&$survey)
{
$this->survey = $survey;
}
public function load ($id)
{
// now a question is loaded
// want to load the survey that this question is in
$this->survey->load($this->get('survey_id')); // ** survey_id is a field in the questions table
// now $this->survey object has all the info about the survey this question is in
}
private function get($name)
{
// returns $name, if isset() from array that load() sets
} }
不过,这让我很头疼,因为似乎 $survey 应该成为 $question 已经是一个完整的对象。但是,如果我在进入对象之前不知道要加载什么survey_id,我该怎么做?我正在改造一个网站,最复杂的部分是散布这个问题。 TIA - 汉斯。
【问题讨论】:
-
$响应?您可能想稍微修改一下您的帖子。
-
是的:脑筋急转弯。我从“回应”开始,然后决定“问题”更容易传达。已编辑。