In PHP, a constructor is a special method that is automatically called when an object is created. It allows you to initialize object properties at the time of object creation.
The constructor method in PHP is named __construct()
.
🔹 Why Use Constructors?
Imagine ordering a pizza 🍕, and it arrives with no toppings—you have to add them manually every time. Annoying, right? A constructor helps you automatically set values when an object is created, so you don’t have to do it manually.
📝 Example 1: Using a Constructor
Let’s create a Car
class where the brand name is set automatically using a constructor.
<?php class Car { public $brand; // Constructor method function __construct($name) { $this->brand = $name; } function getBrand() { return "Car Brand: " . $this->brand; } } // Creating objects with constructor $car1 = new Car("Tesla"); $car2 = new Car("BMW"); echo $car1->getBrand(); // Output: Car Brand: Tesla echo "<br>"; echo $car2->getBrand(); // Output: Car Brand: BMW ?>
As soon as we create a new Car
object, the constructor automatically assigns the brand name. No need for a separate method to set it!
📝 Example 2: Constructor with Default Values
If no brand is specified, we can set a default value in the constructor.
<?php class Laptop { public $brand; function __construct($name = "Unknown") { $this->brand = $name; } function getBrand() { return "Laptop Brand: " . $this->brand; } } // Creating objects with and without parameters $laptop1 = new Laptop("Dell"); $laptop2 = new Laptop(); // No brand specified echo $laptop1->getBrand(); // Output: Laptop Brand: Dell echo "<br>"; echo $laptop2->getBrand(); // Output: Laptop Brand: Unknown ?>
Since we didn’t pass a value for $laptop2
, it gets the default value "Unknown"
.
🔹 Using Multiple Parameters in a Constructor
Constructors can take multiple arguments to initialize multiple properties.
📝 Example 3: Constructor with Multiple Parameters
<?php class Mobile { public $brand; public $price; function __construct($name, $cost) { $this->brand = $name; $this->price = $cost; } function getDetails() { return "Mobile: " . $this->brand . " - Price: $" . $this->price; } } // Creating objects $phone1 = new Mobile("iPhone", 999); $phone2 = new Mobile("Samsung", 799); echo $phone1->getDetails(); echo "<br>"; echo $phone2->getDetails(); ?>
Now each object has both brand
and price
properties set automatically.
🔹 Using Constructors in Inheritance
When a child class extends a parent class, it can inherit or override the constructor.
📝 Example 4: Constructor in an Inherited Class
<?php class Animal { public $name; function __construct($animalName) { $this->name = $animalName; } function getName() { return "Animal: " . $this->name; } } // Child class class Dog extends Animal { public $breed; function __construct($animalName, $dogBreed) { parent::__construct($animalName); // Calling parent constructor $this->breed = $dogBreed; } function getBreed() { return "Breed: " . $this->breed; } } $dog1 = new Dog("Dog", "Labrador"); echo $dog1->getName(); echo "<br>"; echo $dog1->getBreed(); ?>
The Dog
class calls the parent class constructor using parent::__construct()
, so it gets the name
from the Animal
class and adds its own breed
property.
🎯 Key Takeaways
__construct()
is a special method that runs when an object is created.- Constructors help initialize object properties automatically.
- Default values can be set if no arguments are passed.
- Multiple parameters can be used to initialize multiple properties.
- Child classes can inherit or override parent class constructors.
📝 Practice Time!
Try modifying the examples to create your own Book 📖 or Movie 🎬 classes with constructors. Have fun experimenting! 🚀