Nesne bağımlılıklarını enjekte nasıl anlamak

0 Cevap php

Ben bir yöntem başka bir nesnenin bir örneğini yükleyen bir nesne var.

$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
}    }

$ Anket zaten $ gündeme gelmek tam bir nesne olmak gerektiği gibi görünüyor, çünkü bu, olsa, beynimi kızartıyor. Ama, nasıl yapmalıyım, ki ben nesne değilim kadar yük survey_id ne bilmiyorum? Ben bir site yenileştirme yaşıyorum ve en karmaşık kısmı bu konu ile littered olduğunu. TIA - Hans.

0 Cevap