0

Expected Result: I have several echo statements and I should be getting something like this:

index1
Question->addChoice1
Choice1
Choice2
Question->addChoice2
index2

Actual Results:

index1
Question->addChoice1
Choice1
Choice2

Its failing at:

$this.$choices[] = new Choice($obj);

I have no idea why its doing this, but its almost 3AM here so maybe I'm just too brain-dead to see it. please help.

index.php

    $QUESTIONS = array();
    for ($chapter=1; $chapter<=count($max_chapter); $chapter++) {
        $question_qry = "SELECT * FROM $tbl_questions WHERE chapter=$chapter ORDER BY RAND() LIMIT $max_chapter[$chapter]";
        $question_req = mysql_query($question_qry);
        while ($question_obj = mysql_fetch_object($question_req)) {
            $newQuestion = new Question($question_obj);
            $choice_qry = "SELECT * FROM $tbl_choices WHERE question=$question_obj->reference ORDER BY choice ASC;";
            $choice_req = mysql_query($choice_qry);
            while ($choice_obj = mysql_fetch_object($choice_req)) { 
                echo "index1<br>";
                $newQuestion->addChoice($choice_obj);
                echo "index2<br>";
            }
            $QUESTIONS[] = $newQuestion;
            echo '<pre>'; print_r($newQuestion); echo '</pre>';
        }
    }
    shuffle($QUESTIONS);
    echo '<pre>'; print_r($QUESTIONS); echo '</pre>';

question.php

class Question {
    private $chapter;
    private $topic;
    private $reference;
    private $question_name;
    private $question_correct;
    private $special;
    private $pic;
    private $choices = array();

    function __construct($obj) {
        $this->chapter          = $obj->chapter;
        $this->topic            = $obj->topic;
        $this->reference        = $obj->reference;
        $this->question_name    = $obj->question_name;
        $this->question_correct = $obj->question_correct;
        $this->special          = $obj->special;
        $this->pic              = $obj->pic;
    }

    public function addChoice($obj){
        echo "Question->addChoice1<br>";
        $this.$choices[] = new Choice($obj);
        echo "Question->addChoice2<br>";
    }

...

choice.php

class Choice {
    private $chapter;
    private $question;
    private $choice;
    private $choice_name;
    private $choice_explanation;

    function __construct($obj) {
        echo "Choice1<br>";
        $this->chapter            = $obj->chapter;
        $this->question           = $obj->question;
        $this->choice             = $obj->choice;
        $this->choice_name        = $obj->choice_name;
        $this->choice_explanation = $obj->choice_explanation;
        echo "Choice2<br>";
    }

...

Results

1
  • 2
    It seems your syntax is incorrect, try $this->choices[] = new Choice($obj); instead of $this.$choices[] = new Choice($obj); Commented Sep 18, 2020 at 9:55

1 Answer 1

4

In PHP you have to use -> to call object properties, not .. . is concatenation.

So just replace $this.$choices[] with $this->choices[]

Typical typo for people who haven't worked much with PHP, but have experience in other languages.

Sign up to request clarification or add additional context in comments.

2 Comments

that was it.... 1 hour of suffering... 15min to write this up... 60 seconds to get an answer
Glad I have helped you. If you would tick the answer, I would be grateful too :) @DericMccadden

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.