PHP Destructor
In PHP, a destructor is a special method that is automatically called when an object is destroyed or goes out of scope. Destructors are useful for cleaning up resources such as closing files, releasing memory, or closing database connections.
Syntax
The destructor method is defined using the __destruct
keyword. Here is the basic syntax of a destructor in PHP:
class MyClass {
// Constructor
public function __construct() {
echo "Constructor called\n";
}
// Destructor
public function __destruct() {
echo "Destructor called\n";
}
}
// Creating an instance of MyClass
$obj = new MyClass();
Example 1: Basic Destructor
In this example, the destructor will be called when the script ends or when the object is explicitly unset.
class MyClass {
// Constructor
public function __construct() {
echo "Constructor called\n";
}
// Destructor
public function __destruct() {
echo "Destructor called\n";
}
}
// Creating an instance of MyClass
$obj = new MyClass();
// Unsetting the object to trigger the destructor
unset($obj);
echo "Script end\n";
Output:
Constructor called
Destructor called
Script end
Example 2: Destructor with Resource Cleanup
Destructors can be used to clean up resources such as closing a file handle.
class FileHandler {
private $file;
// Constructor
public function __construct($filename) {
$this->file = fopen($filename, 'w');
echo "File opened\n";
}
// Destructor
public function __destruct() {
fclose($this->file);
echo "File closed\n";
}
public function write($data) {
fwrite($this->file, $data);
}
}
// Creating an instance of FileHandler
$fileHandler = new FileHandler('example.txt');
$fileHandler->write("Hello, World!\n");
echo "Script end\n";
Output:
File opened
Script end
File closed
In this example, the file is opened when the FileHandler
object is created, and it is closed automatically when the object is destroyed at the end of the script.
Example 3: Destructor in Inheritance
Destructors in parent and child classes can both be called when an object is destroyed.
class ParentClass {
public function __destruct() {
echo "Parent destructor called\n";
}
}
class ChildClass extends ParentClass {
public function __destruct() {
echo "Child destructor called\n";
parent::__destruct(); // Call parent destructor explicitly if needed
}
}
// Creating an instance of ChildClass
$obj = new ChildClass();
echo "Script end\n";
Output:
Script end
Child destructor called
Parent destructor called
In this example, the destructor of the child class is called first, followed by the destructor of the parent class.
Destructors are a powerful feature in PHP that allows for efficient resource management and cleanup. They ensure that resources are properly released when objects are no longer needed.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.