Constructor & Inheritance in PHP
In the previous lesson, you created classes and objects and manually assigned values to properties.
But that approach has a problem:
$product = new Product(); $product->name = "Laptop"; // Forgot price
π This creates incomplete objects and leads to bugs.
Constructors and inheritance solve this by making object creation structured and reliable.
What is a Constructor?
A constructor is a special method that runs automatically when an object is created.
Creating a Constructor
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
Creating Object with Constructor
$product = new Product("Laptop", 50000);
echo $product->name;
Output:
Laptop
π Now the object is always created with required data.
What Happens If You Skip Constructor Arguments
$product = new Product();
Output (Error):
Fatal error: Too few arguments to function Product::__construct()
π PHP forces you to provide required values β this prevents broken objects.
Default Values in Constructor
class User {
public $name;
public $role;
public function __construct($name, $role = "user") {
$this->name = $name;
$this->role = $role;
}
}
$user = new User("Rohan");
echo $user->role;
Output:
user
What is Inheritance?
Inheritance allows one class to reuse another classβs properties and methods.
π You write code once and reuse it in multiple places.
Inheritance Example (Proper Use)
class Animal {
public function speak() {
echo "Animal sound";
}
}
class Dog extends Animal {
public function bark() {
echo "Bark";
}
}
$dog = new Dog();
$dog->speak(); // inherited
$dog->bark(); // own method
Output:
Animal sound Bark
π Dog reuses behavior and adds its own.
Method Overriding
class Dog extends Animal {
public function speak() {
echo "Bark";
}
}
$dog = new Dog();
$dog->speak();
Output:
Bark
Using Parent Method with parent::
class Dog extends Animal {
public function speak() {
parent::speak();
echo " + Bark";
}
}
$dog = new Dog();
$dog->speak();
Output:
Animal sound + Bark
Constructor in Inheritance (Basic Case)
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
class Dog extends Animal {
}
$dog = new Dog("Tommy");
echo $dog->name;
Output:
Tommy
π Child automatically uses parent constructor.
Constructor in Inheritance (Advanced Case)
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
class Dog extends Animal {
public $breed;
public function __construct($name, $breed) {
parent::__construct($name);
$this->breed = $breed;
}
}
$dog = new Dog("Tommy", "Labrador");
echo $dog->name . " - " . $dog->breed;
Output:
Tommy - Labrador
π Use parent::__construct() when child has extra data.
Common Mistakes Beginners Make
1. Forgetting Constructor Arguments
$product = new Product(); // Error
π Constructor requires values.
2. Not Calling Parent Constructor
class Dog extends Animal {
public function __construct($name) {
// Missing parent::__construct($name)
}
}
Correct:
public function __construct($name) {
parent::__construct($name);
}
3. Thinking Inheritance Copies Code
// Wrong assumption:
class Dog {
public function speak() {
echo "Animal sound";
}
}
π In reality, Dog does NOT copy code β it links to Animal.
π If you update Animal, all child classes automatically reflect the change.
Practice Exercise
Task 1 (Easy)
Create a User class with constructor.
- Pass name = "Rohan"
- Print the name
Expected Output:
Rohan
Task 2 (Medium)
Create a Product class with constructor (name, price).
- Use: "Laptop", 50000
- Print:
Expected Output:
Laptop - βΉ50000
Task 3 (Hard)
Create a Vehicle class:
start()β prints "Vehicle started"
Create Car class:
- extends
Vehicle - override
start()β prints "Car started"
Expected Output:
Car started
Summary
In this lesson, you learned:
- constructors ensure objects are created with required data
- PHP throws an error if constructor arguments are missing
- inheritance allows code reuse across classes
- method overriding changes behavior in child classes
parent::lets you reuse parent logicparent::__construct()is required when extending constructors