Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Inheritance and Polymorphism
Identify the error in this PHP code snippet:
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if ($dog instanceof 'Animal') {
    echo 'It is an animal';
}
AMissing semicolon after class definition
BIncorrect object creation syntax
CUsing quotes around class name in instanceof
DUsing instanceof with a variable instead of object
Step-by-Step Solution
Solution:
  1. Step 1: Check instanceof usage

    The instanceof operator expects a class name without quotes, not a string.
  2. Step 2: Verify other parts

    Classes and object creation are correct; semicolons are present.
  3. Final Answer:

    Using quotes around class name in instanceof -> Option C
  4. Quick Check:

    Class name must be unquoted in instanceof [OK]
Quick Trick: Do not put quotes around class names in instanceof [OK]
Common Mistakes:
  • Putting class names in quotes inside instanceof
  • Confusing instanceof with string comparison
  • Missing semicolons elsewhere (not in this code)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes