Bird
0
0

Identify the error in the following PHP code related to constructor inheritance:

medium📝 Debug Q14 of 15
PHP - Inheritance and Polymorphism
Identify the error in the following PHP code related to constructor inheritance:
class Base {
  public function __construct() {
    echo "Base constructor.";
  }
}
class Derived extends Base {
  public function __construct() {
    parent::construct();
    echo "Derived constructor.";
  }
}
new Derived();
AThe parent constructor is called with wrong method name 'construct()'.
BThe child class should not have a constructor.
CMissing call to parent's constructor causes error.
DNo error; code runs fine.
Step-by-Step Solution
Solution:
  1. Step 1: Check parent constructor call syntax

    The code calls parent::construct(); but the correct method name is __construct() with double underscores.
  2. Step 2: Understand impact of wrong method name

    Because 'construct()' does not exist, PHP will throw a fatal error or warning about undefined method.
  3. Final Answer:

    The parent constructor is called with wrong method name 'construct()'. -> Option A
  4. Quick Check:

    Use parent::__construct(), not parent::construct() [OK]
Quick Trick: Always use double underscore __construct() for constructors [OK]
Common Mistakes:
  • Calling parent::construct() instead of parent::__construct()
  • Forgetting double underscores in constructor name
  • Assuming parent constructor runs without correct call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes