CodeBlanks Logo Icon CodeBlanks

Designing a question class

PHP course · Intermediate · Lesson 43/115

Design a class that holds a question, options and the correct answer.

Setting up a question with the constructor

When you create a question, you want to supply the text, options and the correct answer together. The __construct method runs automatically when an instance is created with new, and assigns this data to properties.

The Question class constructor takes three parameters: string $text, array $options, int $answerIndex. Inside it, you copy the parameters to properties with assignments like $this->text = $text;. So new Question("What is PHP?", [...], 0) gives you a ready question.

Initializing properties with $this

Inside the constructor, the $this pseudo-variable points to the instance being created. To assign a parameter to a property you write $this->text = $text;. You repeat the same pattern for options and answerIndex.

It's good practice to declare the properties in the class first: public array $options = []; and public int $answerIndex = 0;. This makes the type and default explicit, so every instance starts in a consistent state.

Exercise Add the constructor

Add public array $options = []; and public int $answerIndex = 0; properties to the Question class. Then write a public function __construct(string $text, array $options, int $answerIndex) constructor; inside it assign $this->text = $text;, $this->options = $options;, $this->answerIndex = $answerIndex;. Update the instance to $q = new Question("What is PHP?", ["Scripting language", "Database", "Browser"], 0); and change the last line to echo count($q->options);.

Show example solution
Add public array $options = []; and public int $answerIndex = 0; to the class. Write public function __construct(string $text, array $options, int $answerIndex) { $this->text = $text; $this->options = $options; $this->answerIndex = $answerIndex; }. Create an instance with $q = new Question("What is PHP?", ["Scripting language", "Database", "Browser"], 0); and print with echo count($q->options);.
Example solution:
<?php
// Question class skeleton
class Question {
    public string $text = "";
    public array $options = [];
    public int $answerIndex = 0;

    public function __construct(string $text, array $options, int $answerIndex) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }
}

$q = new Question("What is PHP?", ["Scripting language", "Database", "Browser"], 0);
echo count($q->options);

Test yourself

  1. What data does the Question constructor take?

    • Only the question text
    • Question text, options and the correct answer index
    • Only the number of options
    • The class name and file path
    Show answer

    Question text, options and the correct answer index — Correct. The constructor takes the question text, the options array and the correct answer index; it assigns all three to properties.

  2. Add the parameter list to the constructor

    public function __construct(string ____) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }
    Show answer
    public function __construct(string $text, array $options, int $answerIndex) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }
  3. Match the constructor concepts

    __construct
    Constructor name
    $this->text
    Property assignment
    array $options
    Options parameter
    new Question(...)
    Instantiation

All lessons in Intermediate