【问题标题】:My object contains multiple instances of itself, should I be concerned?我的对象包含自身的多个实例,我应该担心吗?
【发布时间】:2012-09-28 12:26:53
【问题描述】:

我有两个班级,QuizQuestion

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

如您所见,我需要让我的 Question 对象访问我的 Quiz 对象值:$quiz_type$personalities

如果我希望加载所有问题及其可能的性格结果,我将使用以下代码:

$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();
}

如果我只想显示一个问题(以及所有个性信息),我可以使用以下代码:

$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();

我担心我的每个 Question 对象都需要来自 Quiz 对象的信息才能发挥作用

当我将所有 Question 对象加载到我的 Quiz 中(使用 $quiz_object->LoadQuestions)时,我的 quiz 对象基本上包含问题信息以及 自身 的实例多次测验的问题。

所以看起来有点像

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

}

所以我的Quiz 对象在对象中被复制了多次。

我应该担心这一点,还是 PHP 是否将我传递的实例仅作为对对象的“引用”来处理?

【问题讨论】:

    标签: php oop object multiple-instances


    【解决方案1】:

    即使您将 same 测验实例分配给多个变量,这绝对不是问题,但完全可以。

    因为您在这里有一个实例,并且每个变量都是访问 那个 实例的一种方式,所以在您的示例中,问题 知道 它们属于哪个测验的。

    在您的设计中,问题需要知道它们属于哪个测验,这就是您所需要的,因此它普遍适用。

    这是标准的 1:n 关系:

       1:n
    
    quiz:questions
    

    注意将关系数量保持在应用程序正常工作所需的最低限度。

    【讨论】:

    • 我有点困惑 :(。当我的问题在 Quiz 对象中时,有办法从我的问题中获取测验信息吗?
    • new Question($question_id, $quiz_object);
    • 所以从它的声音来看,我可以将该对象传递给我想要的尽可能多的问题,并且它不会重复这些值。它只是有一种引用同一个对象的方法?
    • 没错。这就是为什么您也以某种方式将其称为对象的原因。所有问题都参考 same 测验对象。变量个数不算。
    【解决方案2】:

    别担心,因为 PHP 版本 5 传递对象是通过引用完成的。

    【讨论】:

    • -1错误。由于 PHP5 对象是由对象处理程序传递的。请关注youtube.com/watch?v=bxxIXPc9IR8 了解更多详情。
    • tereško 只是烦人,最后通过对象句柄传递意味着对象不重复,并且对对象的引用被传递。
    • @JvdBerg 如何指出引用和对象处理程序是不同的东西,“很烦人”?请阅读php.net/manual/en/language.oop5.references.php
    • @tereško 是一个为那些试图做出贡献的人投反对票的混蛋,不像你那样“知识渊博”,这很烦人。可悲的是,SO 到处都是像你这样的人。你必须学会​​以建设性的方式做出贡献,而不是学究式的态度。
    猜你喜欢
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    相关资源
    最近更新 更多