In PHP constructors and destructors are special methods in a class that automatically run when an object is created or destroyed.
1. Constructor
- Purpose: Initialize object properties or perform setup tasks when an object is created.
- Special Method:
__construct() - Automatically Called: As soon as you create an object using
new.
Example:
<?php
class Car {
public $brand;
// Constructor
public function __construct($brandName) {
$this->brand = $brandName;
echo "Car brand set to: {$this->brand}<br>";
}
}
$car1 = new Car("BMW"); // Constructor is automatically called
$car2 = new Car("Audi");
?>
Output:
Car brand set to: BMW
Car brand set to: Audi
2. Destructor
- Purpose: Cleanup tasks before the object is destroyed (e.g., closing database connections, freeing resources).
- Special Method:
__destruct() - Automatically Called: When there are no more references to the object, or at the end of the script.
Example:
<?php
class Car {
public $brand;
public function __construct($brandName) {
$this->brand = $brandName;
echo "Car brand set to: {$this->brand}<br>";
}
// Destructor
public function __destruct() {
echo "Destroying car object: {$this->brand}<br>";
}
}
$car1 = new Car("BMW");
$car2 = new Car("Audi");
?>
Possible Output:
Car brand set to: BMW
Car brand set to: Audi
Destroying car object: BMW
Destroying car object: Audi
(Destructors may run in any order when script ends.)
Quick Difference Table
| Feature | Constructor (__construct) | Destructor (__destruct) |
|---|---|---|
| When Called | On object creation | On object destruction/end of script |
| Purpose | Initialize / set up | Clean up / close resources |
| Parameters | Can take parameters | No parameters allowed |
| Return Value | Cannot return a value | Cannot return a value |
🎯 Real-World Example — Booking a Hotel Room
Imagine you are checking into a hotel.
Constructor (Check-in)
- When you arrive, the receptionist gives you a key, registers your name, and assigns you a room.
- This is like the constructor — it sets up everything you need before you start using the room.
<?php
class HotelRoom {
public $guestName;
// Constructor = Check-in
public function __construct($name) {
$this->guestName = $name;
echo "Welcome {$this->guestName}, your room is ready!<br>";
}
// Destructor = Check-out
public function __destruct() {
echo "Goodbye {$this->guestName}, your room is now closed.<br>";
}
}
$guest1 = new HotelRoom("John");
$guest2 = new HotelRoom("Alice");
Possible Output:
Welcome John, your room is ready!
Welcome Alice, your room is ready!
Goodbye John, your room is now closed.
Goodbye Alice, your room is now closed.
Destructor (Check-out)
- When you leave, you return the key and the hotel cleans up the room for the next guest.
- This is like the destructor — it closes everything, frees resources, and does final cleanup.
✅ Key takeaway:
- Constructor = Setting things up when you arrive.
- Destructor = Cleaning up before you leave.
Can a PHP class have multiple constructors?
No, PHP does not support method overloading directly.
Can a destructor accept arguments in PHP?
No. The
__destruct() method does not accept any arguments. It is always called automatically without parameters.When is the destructor called in PHP?
When script execution ends
When the object is explicitly destroyed using
When the object goes out of scope
When the object is explicitly destroyed using
unset($obj)When the object goes out of scope
Can we call a constructor or destructor manually?
Yes, but it’s not recommended.
You can call
You can call
$obj->__construct() or $obj->__destruct(), but normally they are managed automatically by PHP.What happens if a class does not have a constructor?
If no constructor is defined, PHP does not call any default method. The object will be created without initialization.
Are constructors/destructors inherited in PHP?
Yes, if a subclass does not define its own
If a subclass has its own constructor, you need to explicitly call the parent constructor using
class ParentClass {
public function __construct() {
echo “Parent constructor\n”;
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct();
echo “Child constructor\n”;
}
}
$child = new ChildClass();
__construct(), it will inherit the parent’s constructor.If a subclass has its own constructor, you need to explicitly call the parent constructor using
parent::__construct().class ParentClass {
public function __construct() {
echo “Parent constructor\n”;
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct();
echo “Child constructor\n”;
}
}
$child = new ChildClass();
What are common real-life use cases of destructors?
Closing database connections
Releasing file handles
Freeing memory/resources
Writing final log messages before object destruction
Releasing file handles
Freeing memory/resources
Writing final log messages before object destruction
