Bird
0
0

What happens if a child class in PHP does not define its own constructor but the parent class has one?

easy📝 Conceptual Q11 of 15
PHP - Inheritance and Polymorphism
What happens if a child class in PHP does not define its own constructor but the parent class has one?
APHP throws an error because the child lacks a constructor.
BNo constructor is called at all.
CThe parent's constructor is automatically called when creating a child object.
DThe child class inherits the parent's constructor as its own.
Step-by-Step Solution
Solution:
  1. Step 1: Understand default constructor behavior

    If the child class does not define a constructor, PHP automatically calls the parent's constructor when creating a child object.
  2. Step 2: Confirm no error or override

    Since the child has no constructor, no error occurs and the parent's constructor runs as expected.
  3. Final Answer:

    The parent's constructor is automatically called when creating a child object. -> Option C
  4. Quick Check:

    Child without constructor calls parent constructor [OK]
Quick Trick: No child constructor? Parent constructor runs automatically [OK]
Common Mistakes:
  • Thinking child constructor must exist to call parent constructor
  • Assuming PHP throws error if child lacks constructor
  • Believing child inherits parent's constructor as a method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes