Bird
Raised Fist0
PHPprogramming~10 mins

Constructor inheritance in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constructor inheritance
Define Parent class with __construct
↓
Define Child class
↓
Child inherits Parent's __construct?
↓
Child calls Parent::__construct
↓
Create Child object
↓
Parent __construct runs
Shows how a child class can inherit or override the parent's constructor and how object creation triggers these constructors.
Execution Sample
PHP
<?php
class ParentClass {
  public function __construct() {
    echo "Parent constructor called\n";
  }
}

class ChildClass extends ParentClass {
}

new ChildClass();
This code shows a child class inheriting the parent's constructor and calling it when a child object is created.
Execution Table
StepActionEvaluationOutput
1Define ParentClass with __constructParentClass::__construct exists
2Define ChildClass extends ParentClassChildClass inherits ParentClass
3Create new ChildClass objectChildClass has no __construct, inherits ParentClass::__construct
4Call ParentClass::__construct via ChildClass objectParentClass::__construct runsParent constructor called
5End of scriptNo more code to run
💡 Script ends after constructor output
Variable Tracker
VariableStartAfter Object CreationFinal
ChildClass objectundefinedobject createdobject exists
Key Moments - 2 Insights
Why does the parent's constructor run when creating a child object if the child has no constructor?
Because the child class inherits the parent's constructor automatically if it does not define its own, as shown in step 3 and 4 of the execution_table.
What happens if the child class defines its own constructor?
The child's constructor overrides the parent's, so only the child's constructor runs unless it explicitly calls parent::__construct(), which is not shown in this example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is output at step 4?
AChild constructor called
BParent constructor called
CNo output
DError
💡 Hint
Check the Output column at step 4 in the execution_table
At which step is the ChildClass object created?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the Action column for object creation in the execution_table
If ChildClass had its own __construct method, what would happen when creating a ChildClass object?
AChild constructor runs instead of parent
BParent constructor runs automatically
CBoth constructors run automatically
DNo constructor runs
💡 Hint
Refer to key_moments about overriding constructors
Concept Snapshot
Constructor inheritance in PHP:
- Child class inherits parent's __construct if none defined
- Creating child object calls parent's constructor if inherited
- Child can override __construct to customize
- To run both, child must call parent::__construct() explicitly
Full Transcript
This example shows how constructor inheritance works in PHP. The ParentClass defines a __construct method that prints a message. The ChildClass extends ParentClass but does not define its own constructor. When we create a new ChildClass object, PHP uses the parent's constructor automatically. The execution table traces these steps: defining classes, creating the object, and running the constructor. The variable tracker shows the child object being created. Key moments clarify why the parent's constructor runs and what happens if the child defines its own constructor. The quiz tests understanding of output, object creation step, and constructor overriding behavior.

Practice

(1/5)
1. What happens if a child class in PHP does not define its own constructor but the parent class has one?
easy
A. PHP throws an error because the child lacks a constructor.
B. No constructor is called at all.
C. The parent's constructor is automatically called when creating a child object.
D. The child class inherits the parent's constructor as its own.

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]
Hint: 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
2. Which of the following is the correct syntax to call a parent class constructor inside a child class constructor in PHP?
easy
A. parent::__construct();
B. parent::constructor();
C. super::__construct();
D. parent->__construct();

Solution

  1. Step 1: Recall PHP parent constructor call syntax

    In PHP, the parent's constructor is called using parent::__construct(); inside the child's constructor.
  2. Step 2: Eliminate incorrect syntax

    Options A, C, and D use invalid syntax: 'constructor' is not the method name, 'super' is not PHP keyword, and 'parent->' is incorrect.
  3. Final Answer:

    parent::__construct(); -> Option A
  4. Quick Check:

    Use parent::__construct() to call parent constructor [OK]
Hint: Use parent::__construct() exactly to call parent constructor [OK]
Common Mistakes:
  • Using 'super' keyword instead of 'parent'
  • Calling parent constructor as parent::constructor()
  • Using object operator '->' with parent keyword
3. What will be the output of the following PHP code?
class ParentClass {
  public function __construct() {
    echo "Parent constructor called. ";
  }
}
class ChildClass extends ParentClass {
  public function __construct() {
    parent::__construct();
    echo "Child constructor called.";
  }
}
new ChildClass();
medium
A. Child constructor called.
B. Parent constructor called. Child constructor called.
C. Parent constructor called.
D. No output.

Solution

  1. Step 1: Trace constructor calls

    The child class constructor calls parent::__construct(); first, so the parent's constructor runs and prints "Parent constructor called. "
  2. Step 2: Continue child constructor execution

    After the parent's constructor, the child constructor prints "Child constructor called." immediately after.
  3. Final Answer:

    Parent constructor called. Child constructor called. -> Option B
  4. Quick Check:

    Child calls parent constructor then prints own message [OK]
Hint: parent::__construct() runs parent's constructor first [OK]
Common Mistakes:
  • Ignoring parent::__construct() call in child constructor
  • Assuming only child constructor output appears
  • Thinking parent's constructor is not called explicitly
4. 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();
medium
A. The parent constructor is called with wrong method name 'construct()'.
B. The child class should not have a constructor.
C. Missing call to parent's constructor causes error.
D. No error; code runs fine.

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]
Hint: 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
5. Given the following classes, what will be the output when creating a new Child object?
class Parent {
  public function __construct() {
    echo "Parent start. ";
  }
}
class Child extends Parent {
  public function __construct() {
    echo "Child start. ";
    parent::__construct();
    echo "Child end.";
  }
}
new Child();
hard
A. Parent start. Child end.
B. Parent start. Child start. Child end.
C. Child start. Child end.
D. Child start. Parent start. Child end.

Solution

  1. Step 1: Follow the order of echo statements

    The child constructor first prints "Child start. ". Then it calls parent::__construct(); which prints "Parent start. ". Finally, the child prints "Child end.".
  2. Step 2: Combine outputs in sequence

    Putting all together, the output is "Child start. Parent start. Child end." exactly in that order.
  3. Final Answer:

    Child start. Parent start. Child end. -> Option D
  4. Quick Check:

    Child prints before and after parent constructor call [OK]
Hint: Order matters: child's code runs before and after parent::__construct() [OK]
Common Mistakes:
  • Assuming parent constructor runs before child's code
  • Ignoring order of echo statements
  • Thinking parent's constructor output appears last
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard