Bird
0
0

Identify the mistake in the following PHP code snippet:

medium📝 Debug Q7 of 15
PHP - Inheritance and Polymorphism
Identify the mistake in the following PHP code snippet:
class Base {
  public function __construct() {
    echo "Base initialized\n";
  }
}
class Derived extends Base {
  public function __construct() {
    parent::__construct;
    echo "Derived initialized\n";
  }
}
AIncorrect class inheritance syntax
BMissing parentheses when calling parent constructor
CConstructor name should be the class name
DEcho statements cannot be used in constructors
Step-by-Step Solution
Solution:
  1. Step 1: Review parent constructor call

    The code uses parent::__construct; which lacks parentheses.
  2. Step 2: Understand correct syntax

    Parent constructor must be called as a method: parent::__construct();
  3. Step 3: Check other options

    Inheritance syntax is correct, constructor naming is correct (using __construct), and echo is valid inside constructors.
  4. Final Answer:

    Missing parentheses when calling parent constructor -> Option B
  5. Quick Check:

    Always include parentheses when calling parent::__construct() [OK]
Quick Trick: Always use parentheses when calling parent::__construct() [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Confusing constructor naming conventions
  • Misusing inheritance syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes