I have two classes: Quiz and Question :
class Quiz() { public $quiz_id; public $quiz_name; // Arrays of objects public $questions; public $personalities; function __construct($quiz_id) { // Sets the basic data of the quiz $this->quiz_id = $quiz_id; $this->quiz_name = $quiz_name_from_db; } function LoadQuestions() { // Get question ids from database // Blank array to add questions to $array_of_question_objects = array(); foreach ($question_from_db AS $question_info) { // Create Question object, passing this Quiz object $question_object = new Question($question_info["question_id"], $this); // Add the question to the array $array_of_question_objects += $question_object; } // Set the array within the Quiz object $this->questions = $array_of_question_objects; } function LoadPersonalities() { $this->personalities = $array_of_personalty_objects; } } class Question() { public $question; // Quiz object within the question for access to personalities and quiz type private $quiz_object; function __construct($question_id, $quiz_object) { // Set the quiz object within the question $this->quiz_object; // Load the question information from the database and set the values of the class $this->question = $question_from_database; } function ShowQuestion() { // Show a question on the page and also // Loop through all of the personalities echo "<div id="question">"; if ($quiz_object->quiz_type == "personality") { foreach ($quiz_object->personalities AS $quiz_personality) { // Show each personality echo $quiz_personality; } } } }
As you can see, I need to give my Question objects access to my Quiz object values: $quiz_type and $personalities .
If I want to download all the questions and with their possible individual results, I will use the following code:
$quiz_id = 1; // Create a quiz object $quiz_object = new Quiz($quiz_id); // Load the questions in to the Quiz object $quiz_object->LoadQuestions(); // Load all of the personalities $quiz_object->LoadPersonalities(); // Show the questions foreach ($quiz_object->questions AS $question) { // Show the question $question->ShowQuestion(); }
If I want to show only one question (and all information about the person), I can use the following code:
$quiz_id = 1; $question_id = 40; // Create a quiz object $quiz_object = new Quiz($quiz_id); // Load all of the personalities for the quiz object $quiz_object->LoadPersonalities(); // Load the question as an object $question_object = new Question($question_id, $quiz_object); // Show the HTML for the question $question_object->ShowQuestion();
I am concerned that every object in my Question needs information from a Quiz object in order to function .
When I load all the Question objects into my quiz (using $ quiz_object-> LoadQuestions), my quiz object essentially contains the request information, as well as an instance several times for each quiz question.
So this is a bit like
Quiz_object {
Question_object: { question information instance of `Quiz_object` } Question_object: { question information instance of `Quiz_object` } Question_object: { question information instance of `Quiz_object` } Question_object: { question information instance of `Quiz_object` } Question_object: { question information instance of `Quiz_object` }
}
So, my Quiz object is duplicated several times inside the object.
Should I be worried about this, or is PHP handling the instance that I am passing as just an object reference?