Bird
0
0

Identify the error in this PHP class method:

medium📝 Debug Q14 of 15
PHP - Classes and Objects
Identify the error in this PHP class method:
class Car {
  public $model = 'Tesla';
  public function getModel() {
    return $model;
  }
}
AMissing <code>$this</code> when accessing <code>$model</code>
BMethod name should be <code>GetModel</code> with capital G
CProperty <code>$model</code> must be private
DReturn statement should be <code>return $this->getModel;</code>
Step-by-Step Solution
Solution:
  1. Step 1: Check property access inside method

    Inside class methods, properties must be accessed with $this->propertyName. Here, $model is used without $this->.
  2. Step 2: Validate other options

    Method names are case-insensitive in PHP, property visibility is not an error here, and the suggested return $this->getModel; is incorrect.
  3. Final Answer:

    Missing $this when accessing $model -> Option A
  4. Quick Check:

    Use $this->property to access properties [OK]
Quick Trick: Always use $this-> to access object properties inside methods [OK]
Common Mistakes:
  • Accessing properties without $this->
  • Confusing method name case sensitivity
  • Misunderstanding property visibility rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes