PHP
Project final - Quiz app
Combine all parts and finalize a working quiz app.
Combine all parts
In the final lesson you'll combine all parts. The Question class (properties, constructor, isCorrect) and the Quiz class (questions, score) are ready. Now you'll add a method that summarizes the quiz app and prints the result.
You'll add a summary(array $answers): string method to the Quiz class. This method gets the correct count with $this->score($answers) and the total with count($this->questions), then returns a summary like "2/2". The user sees the score in one line.
Summary output with the summary method
To call the summary method you write echo $quiz->summary([0, 1]);. This gives an output like "2/2": the first number is correct answers, the second is the total questions.
Inside the method you get the score with $correct = $this->score($answers);, find the total with $total = count($this->questions);, and return them joined with return $correct . "/" . $total;. This is a nice example of reusing the existing score method: you call it instead of rewriting it.
Exercise Add the summary method and run the quiz
Add a public function summary(array $answers): string method to the Quiz class. Inside it get the score with $correct = $this->score($answers);, find the total with $total = count($this->questions); and return the summary with return $correct . "/" . $total;. Replace the last line echo $quiz->score([0, 1]); with echo $quiz->summary([0, 1]);.
Show example solution
string { $correct = $this->score($answers); $total = count($this->questions); return $correct . "/" . $total; } to the Quiz class. Change the last echo line to echo $quiz->summary([0, 1]);.
Example solution:
<?php
// Combine all parts: Question + Quiz + summary
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;
}
public function isCorrect(int $choice): bool {
return $choice === $this->answerIndex;
}
}
class Quiz {
public array $questions;
public function __construct(array $questions = []) {
$this->questions = $questions;
}
public function score(array $answers): int {
$correct = 0;
foreach ($this->questions as $i => $q) {
if ($q->isCorrect($answers[$i] ?? -1)) {
$correct++;
}
}
return $correct;
}
public function summary(array $answers): string {
$correct = $this->score($answers);
$total = count($this->questions);
return $correct . "/" . $total;
}
}
$quiz = new Quiz([
new Question("What is PHP?", ["Scripting language", "Database", "Browser"], 0),
new Question("Which is a loop?", ["if", "for", "echo"], 1),
]);
echo $quiz->summary([0, 1]);
Project complete
Congratulations! You finished the quiz app project. Across five lessons you designed a Question class with properties, a constructor and an isCorrect method. Then you added a Quiz class with a foreach loop and the score and summary methods.
You can now reuse these classes in other projects: a question bank, an online test tool or an educational game. Bundling data and behavior with OOP makes the code reusable and easier to maintain.
Test yourself
-
What's needed to run the quiz app?
- Define Question and Quiz classes, instantiate and call methods
- Just write an echo statement
- Set up a database connection
- The quiz cannot run without an HTML form
Show answer
Define Question and Quiz classes, instantiate and call methods — Correct. Defining the classes, instantiating and calling methods is enough; the quiz runs in the console.
-
Match the quiz app concepts
- Question
- Question class
- Quiz
- Class holding questions
- score()
- Returns correct count
- summary()
- Summarizes the score
-
Complete the summary return statement
public function summary(array $answers): string {
$correct = $this->score($answers);
$total = count($this->questions);
return ____;
}
Show answer
public function summary(array $answers): string {
$correct = $this->score($answers);
$total = count($this->questions);
return $correct . "/" . $total;
}
Next lesson: Defining interfaces