Bird
0
0

You have a parent class with a constructor that accepts parameters. How should the child class constructor be defined to properly inherit and extend the parent constructor in PHP?

hard📝 Application Q8 of 15
PHP - Inheritance and Polymorphism
You have a parent class with a constructor that accepts parameters. How should the child class constructor be defined to properly inherit and extend the parent constructor in PHP?
ADefine child constructor with matching parameters and call parent::__construct() with those parameters.
BDefine child constructor without parameters and call parent::__construct() without arguments.
CDo not define a child constructor; PHP will automatically pass parameters to parent.
DDefine child constructor with different parameters and do not call parent::__construct().
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameter passing in constructors

    If parent constructor requires parameters, child constructor must accept and pass them explicitly.
  2. Step 2: Define child constructor accordingly

    Child constructor should have matching parameters and call parent::__construct() with those parameters.
  3. Final Answer:

    Define child constructor with matching parameters and call parent::__construct() with those parameters. -> Option A
  4. Quick Check:

    Pass parameters explicitly to parent::__construct() [OK]
Quick Trick: Match parameters and call parent::__construct(params) [OK]
Common Mistakes:
  • Not passing parameters to parent constructor
  • Assuming automatic parameter forwarding
  • Defining child constructor without needed parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes