In PHP object oriented programming, how do you call the constructor of a parent class from within a child class constructor?

Difficulty: Easy

Correct Answer: Inside the child constructor, call parent::__construct(); optionally passing any required arguments, to invoke the parent class constructor.

Explanation:


Introduction / Context:
In object oriented PHP, classes can extend other classes and inherit their constructors. Sometimes a child class needs to perform its own initialisation but still wants to reuse logic from the parent constructor. This question tests whether you know the correct syntax for calling a parent constructor explicitly.


Given Data / Assumptions:

    • We have a parent class with a constructor method, usually named __construct().
    • A child class extends the parent and defines its own __construct() method.
    • The child wants to reuse the parent initialisation code.


Concept / Approach:
PHP provides the parent keyword to refer to the immediate parent class. Combined with the scope resolution operator :: and the __construct method name, parent::__construct() calls the parent class constructor. You can pass arguments inside the parentheses if the parent constructor requires parameters. This pattern is widely used in frameworks and custom code whenever inheritance is involved.


Step-by-Step Solution:
Step 1: Define a parent class such as class Base { public function __construct($x) { /* base setup */ } }.Step 2: Define a child class such as class Child extends Base { public function __construct($x, $y) { parent::__construct($x); /* child setup using $y */ } }.Step 3: In this child constructor, parent::__construct($x); calls the Base constructor and passes the argument along.Step 4: The child can then perform its own additional initialisation after the call to parent::__construct().Step 5: Option A describes this pattern correctly and is therefore the correct answer.


Verification / Alternative check:
Running a simple example with echo statements inside both constructors shows the order of execution. When you instantiate the Child class, the script prints the parent constructor message first, followed by the child constructor message, confirming that parent::__construct() was invoked successfully from within the child.


Why Other Options Are Wrong:
Option B suggests using $this->parent(), which is not valid syntax in PHP. Option C claims that calling the parent constructor is impossible, which is false. Option D suggests using include as a substitute, which completely misunderstands the role of constructors and inheritance.


Common Pitfalls:
One pitfall is forgetting to call parent::__construct() when the parent constructor performs important tasks such as setting up dependencies or initial property values. This can lead to partially initialised objects. Another is attempting to call the old style constructor named after the class in modern PHP code when the class uses __construct(), which can be confusing in legacy projects.


Final Answer:
Inside the child constructor, call parent::__construct(); optionally passing any required arguments, to invoke the parent class constructor.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion