Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:56:54+00:00 2026-06-12T01:56:54+00:00

I have two classes, Quiz and Question : class Quiz() { public $quiz_id; public

  • 0

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 objects values: $quiz_type and $personalities.

If I wish to load all questions and with their possible personality outcomes 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 wish to show just one question (and all personality information), 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 each of my Question objects needs information from the Quiz object in order to function.

When I load all of the Question objects in to my Quiz (using $quiz_object->LoadQuestions), my quiz object essentially contains the question information as well as an instance of itself multiple times for each question of the quiz.

So it looks a little 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 multiple times within the object.

Should I be concerned about this, or does PHP handle the instance that I pass around as simply a “reference” to the object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:56:56+00:00Added an answer on June 12, 2026 at 1:56 am

    Even you assign the same quiz instance to multiple variables, this must not be an issue at all but can perfectly okay.

    Because you have a single instance here, and each variable is a way of accessing that one instance, here in your example the questions know of which quiz they are part of.

    As in your design the questions need to know to which quiz they belong to, this is what you need so it’s generally applicable.

    It is a standard 1:n relationship:

       1:n
    
    quiz:questions
    

    Take care that you keep the number of relations to the needed minimum for your application to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes class validate { public $mediaFlag; function ValidateTypeOfMedia($SOEmag,$SOEtab,$Soct,$DAL,$insertMeda,$other){ if($SOEmag==){ return Must
I have two classes : import android.cla; public class CW { public static void
I have two classes: public class Singleton{ private Singleton(){...} private static class InstanceHolder{ private
I have two classes, one nested in the other. Public Class Operation Public Property
I have two classes class PopulationMember { public: void operationOnThisMember1(); void operationOnThisMember2(); ... private:
I have two classes: class Player { public string Id { set; get; }
I have two classes. UserModel and UserController. file: localhost/controllers/user.controller.php <?php class UserController { public
I have two classes below: public class Module { public int Id { get;
I have two classes defined like public class PostleitzahlList : ObservableCollection<Postleitzahl> { } public
I have two classes declared in C# code: public class A { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.