📖 Scenario: Imagine you are creating a simple system to manage different types of vehicles. Each vehicle has a brand and a model. Cars are a special type of vehicle that also have a number of doors.
🎯 Goal: You will create a base class Vehicle with a constructor to set brand and model. Then create a subclass Car that inherits from Vehicle and adds the number of doors. You will use constructor inheritance to reuse the base class constructor.
📋 What You'll Learn
Create a class Vehicle with a constructor that takes $brand and $model and sets them as properties.
Create a subclass Car that extends Vehicle.
In Car, create a constructor that takes $brand, $model, and $doors.
Use parent::__construct($brand, $model) inside Car constructor to call the base constructor.
Add a property $doors in Car and set it in the constructor.
Create an instance of Car with brand 'Toyota', model 'Corolla', and 4 doors.
Print the brand, model, and doors of the car.
💡 Why This Matters
🌍 Real World
Constructor inheritance is used in many real-world PHP applications to reuse code and organize related classes efficiently, such as in vehicle management systems, user roles, or product categories.
💼 Career
Understanding constructor inheritance is important for PHP developers working on object-oriented projects, frameworks, or any codebase that uses class hierarchies.
Progress0 / 4 steps
1
Create the base class Vehicle
Create a class called Vehicle with a constructor that takes parameters $brand and $model. Inside the constructor, set these parameters to properties $this->brand and $this->model.
PHP
Hint
Use class Vehicle { ... } and inside it define public function __construct($brand, $model).
2
Create subclass Car with constructor
Create a class called Car that extends Vehicle. Add a constructor that takes $brand, $model, and $doors. Inside the constructor, call parent::__construct($brand, $model) to reuse the base constructor. Also set $this->doors to $doors.
PHP
Hint
Use class Car extends Vehicle and inside the constructor call parent::__construct($brand, $model).
3
Create an instance of Car
Create a variable called $myCar and set it to a new Car object with brand 'Toyota', model 'Corolla', and 4 doors.
PHP
Hint
Use $myCar = new Car('Toyota', 'Corolla', 4); to create the object.
4
Print the car details
Print the brand, model, and number of doors of $myCar using echo. Format the output exactly as: Brand: Toyota, Model: Corolla, Doors: 4
PHP
Hint
Use echo "Brand: {$myCar->brand}, Model: {$myCar->model}, Doors: {$myCar->doors}"; to print the details.
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
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.
Step 2: Confirm no error or override
Since the child has no constructor, no error occurs and the parent's constructor runs as expected.
Final Answer:
The parent's constructor is automatically called when creating a child object. -> Option C
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
Step 1: Recall PHP parent constructor call syntax
In PHP, the parent's constructor is called using parent::__construct(); inside the child's constructor.
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.
Final Answer:
parent::__construct(); -> Option A
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
Step 1: Trace constructor calls
The child class constructor calls parent::__construct(); first, so the parent's constructor runs and prints "Parent constructor called. "
Step 2: Continue child constructor execution
After the parent's constructor, the child constructor prints "Child constructor called." immediately after.
Final Answer:
Parent constructor called. Child constructor called. -> Option B
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
Step 1: Check parent constructor call syntax
The code calls parent::construct(); but the correct method name is __construct() with double underscores.
Step 2: Understand impact of wrong method name
Because 'construct()' does not exist, PHP will throw a fatal error or warning about undefined method.
Final Answer:
The parent constructor is called with wrong method name 'construct()'. -> Option A
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
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.".
Step 2: Combine outputs in sequence
Putting all together, the output is "Child start. Parent start. Child end." exactly in that order.
Final Answer:
Child start. Parent start. Child end. -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.