Bird
0
0

Find the error in this PHP constructor code:

medium📝 Debug Q7 of 15
PHP - Classes and Objects
Find the error in this PHP constructor code:
class Animal {
  public $type;
  public function __construct() {
    $this->type = 'Dog'
  }
}
AMissing semicolon after assignment
BConstructor name is wrong
CProperty type is not declared
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax inside constructor

    The assignment line lacks a semicolon at the end.
  2. Step 2: Identify syntax error impact

    Missing semicolon causes a syntax error and prevents code from running.
  3. Final Answer:

    Missing semicolon after assignment -> Option A
  4. Quick Check:

    Every statement must end with semicolon [OK]
Quick Trick: Always end statements with semicolon in PHP [OK]
Common Mistakes:
  • Forgetting semicolon after statements
  • Confusing constructor name with __construct
  • Thinking property declaration is optional error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes