Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q6 of 15
PHP - Classes and Objects

Find the error in this code snippet:

class Animal {
public $type;
}
$a = new Animal;
echo $a->type;
AMissing parentheses after class name in instantiation.
BCannot echo object property directly.
CClass name should be lowercase.
DProperty <code>type</code> is not initialized before use.
Step-by-Step Solution
Solution:
  1. Step 1: Check object creation syntax

    $a = new Animal; is valid in PHP; parentheses are optional if no constructor arguments.
  2. Step 2: Check property initialization

    The property type is declared but not set, so echoing it outputs nothing or null.
  3. Final Answer:

    Property type is not initialized before use. -> Option D
  4. Quick Check:

    Uninitialized property outputs null [OK]
Quick Trick: Initialize properties before echoing to avoid empty output [OK]
Common Mistakes:
  • Assuming parentheses always required
  • Expecting error for uninitialized property
  • Thinking class name case matters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes