Introduction
In Object-Oriented Programming (OOP), a destructor is a special type of method that is automatically called when an object is destroyed or removed from memory. In PHP, destructors are used to perform cleanup tasks when an object is no longer needed.
PHP introduced destructors in PHP 5, similar to other programming languages such as C++ and Java. The destructor method in PHP is named __destruct() and it is automatically executed when the object is destroyed.
- Closing database connections
- Releasing file resources
- Cleaning up memory
- Saving object state before destruction
Constructors and destructors do not accept parameters, and they do not return values. Unlike
- A destructor is automatically called when the object is deleted or script execution ends.
- The destructor method is defined using __destruct().
- It does not accept parameters.
Using destructors helps developers manage system resources efficiently and prevent memory or resource leaks.
Syntax
The syntax for creating a destructor in PHP is simple.
class ClassName {
function __destruct() {
// cleanup code
}
}
Explanation:
- __destruct() β special method name for destructor
- It is automatically called when the object is destroyed.
- It does not require parameters.
Example of creating an object:
$obj = new ClassName();
When the script finishes or the object is removed, the destructor runs automatically.
Example
Below is a simple example demonstrating a destructor.
<?php
class Test {
function __construct() {
echo "Object created
";
}
function __destruct() {
echo "Object destroyed";
}
}
$obj = new Test();
?>
Output:
Object destroyed
In this example:
- The constructor runs when the object is created.
- The destructor runs automatically when the script finishes.
Explanation
Letβs understand how destructors work step by step.
First, we define a class.
class Test
Inside the class, we create a constructor.
function __construct() {
echo "Object created";
}
This constructor runs when the object is created.
Next, we define the destructor.
function __destruct() {
echo "Object destroyed";
}
This method runs automatically when:
- The object goes out of scope.
- The script finishes execution
- The object is manually destroyed.
Then we create an object.
$obj = new Test();
When the script reaches the end, PHP automatically calls the destructor.
Real-world Example
Letβs look at a real-world example where a destructor is used to close a database connection.
<?php
class Database {
private $link;
public function __construct() {
$this->link = mysqli_connect("localhost", "root", "", "testdb");
if(!$this->link) {
die("Database connection failed");
}
echo "Database connected
";
}
public function __destruct() {
mysqli_close($this->link);
echo "Database connection closed";
}
}
$db = new Database();
?>
Output:
Database connection closed
In this example:
- The constructor connects to the database.
- The destructor automatically closes the connection when the script finishes.
This ensures that database resources are released properly.
Why use destructors?
Destructors are useful for performing cleanup tasks, such as:
- Closing database connections
- Closing file handles
- Releasing resources
- Logging object destruction
Example from database connection scenario:
function __destruct() {
mysqli_close($this->link);
}
This ensures that the database connection is closed when the object is destroyed.
Without a destructor, the connection might remain open until PHP automatically clears it.
PHP Destructor β Interview Questions
Q 1: What is a destructor?
Q 2: Destructor name?
Q 3: When is destructor executed?
Q 4: Can destructor accept parameters?
Q 5: Why use destructor?
Conclusion
PHP destructors are an important feature of Object-Oriented Programming that help manage resources efficiently. A destructor is automatically executed when an object is destroyed, making it ideal for performing cleanup tasks such as closing database connections or releasing memory.
By using destructors, developers can ensure that resources are properly released when objects are no longer needed. This helps improve application performance and prevents potential memory or resource leaks.
Understanding how destructors work is important when building large applications, especially those involving database connections, file handling, and system resources.
Along with constructors, destructors form an essential part of PHP OOP concepts and help developers build clean, efficient, and well-structured applications.
PHP Destructor β Objective Questions (MCQs)
Q1. Destructor in PHP is used to ______.
Q2. Destructor method name in PHP is ______.
Q3. Destructor is called ______.
Q4. Can destructor accept parameters?
Q5. Destructor is useful for ______.