PHP : OOP - Destructor

In PHP, a destructor is a special method within a class that is automatically called when an object of that class is destroyed or goes out of scope. The purpose of a destructor is to perform any cleanup or release of resources that the object may have acquired during its lifetime. Destructors are defined using the __destruct keyword.

Destructor Syntax:


class ClassName {
    public function __destruct() {
        // Destructor code
    }
}

 

Example of a Destructor:

Here's an example to illustrate how destructors work in PHP:


class MyClass {
    public function __construct() {
        echo "Object created.<br>";
    }

    public function __destruct() {
        echo "Object destroyed.<br>";
    }
}

// Creating an object of the MyClass class
$obj1 = new MyClass();

// Code execution

// The object is destroyed when it goes out of scope or unset
unset($obj1); // Output: Object destroyed.

 

In this example:

  • The MyClass class has a constructor __construct that echoes Object created. when an object of the class is created.
  • It also has a destructor __destruct that echoes Object destroyed. when the object is destroyed.
  • After creating the object $obj1, it is explicitly destroyed using unset($obj1), which triggers the destructor.

Automatic Destruction:

Objects in PHP are automatically destroyed when they are no longer referenced or when the script finishes execution. PHP's garbage collector automatically deallocates memory and calls the destructor.

Example:


class MyClass {
    public function __construct() {
        echo "Object created.<br>";
    }

    public function __destruct() {
        echo "Object destroyed.<br>";
    }
}

// Creating objects and allowing them to be destroyed automatically
$obj1 = new MyClass(); // Output: Object created.
$obj2 = new MyClass(); // Output: Object created.
// No need to explicitly destroy the objects

// PHP will automatically destroy the objects at the end of script execution
 

Using Destructors for Cleanup:

Destructors are useful for releasing resources such as closing files, database connections, or freeing up memory. They can be particularly important when dealing with external resources that need to be properly released.

Example with File Handling:


class FileHandler {
    private $fileHandle;

    public function __construct($filename) {
        $this->fileHandle = fopen($filename, 'r');
    }

    public function readFile() {
        if ($this->fileHandle) {
            while (!feof($this->fileHandle)) {
                echo fgets($this->fileHandle) . "<br>";
            }
        }
    }

    public function __destruct() {
        if ($this->fileHandle) {
            fclose($this->fileHandle);
            echo "File handle closed.<br>";
        }
    }
}

// Creating an object of the FileHandler class
$file = new FileHandler("example.txt");

// Reading and displaying the contents of the file
$file->readFile();

// The file handle will be automatically closed when the object is destroyed
 

Important Notes:

  • Destructors are not guaranteed to execute in any specific order.
  • Objects are destroyed when they go out of scope, unset, or when the script finishes execution.
  • It's good practice to release resources explicitly in the destructor, especially for database connections, file handles, etc.

Conclusion:

Destructors in PHP are special methods that are automatically called when an object is destroyed or goes out of scope. They are used to perform cleanup tasks such as releasing resources like closing files or database connections. While PHP automatically deallocates memory and calls destructors, it's good practice to explicitly release resources in the destructor, especially when dealing with external resources. Understanding destructors is important for managing resources efficiently in object-oriented PHP programs.