Bird
0
0

Identify the error in this PHP class:

medium📝 Debug Q6 of 15
PHP - Classes and Objects
Identify the error in this PHP class:
class Book {
  private $title;
  public function __construct($title) {
    $this->title = $title;
  }
  public function getTitle() {
    return title;
  }
}
ANo error, code is correct
BMissing $ before title in return statement
CProperty $title should be public
DConstructor name is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Review the getTitle method

    The method returns 'title' without the $ sign, which means it returns a constant or string, not the property.
  2. Step 2: Correct the return statement

    It should return $this->title to access the property correctly.
  3. Final Answer:

    Missing $ before title in return statement -> Option B
  4. Quick Check:

    Return property requires $this->property [OK]
Quick Trick: Use $this->property to access class properties inside methods [OK]
Common Mistakes:
  • Forgetting $ in return statement
  • Thinking constructor name is wrong
  • Assuming property visibility causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes