PHP Destructors

In PHP, a destructor is a special method that is called automatically when an object is destroyed. It helps free up resources like database connections, file handles, or memory.

The destructor method in PHP is named __destruct().


๐Ÿ”น Why Use Destructors?

Imagine finishing your meal at a restaurant ๐Ÿฝ๏ธ but leaving your table messy. A destructor acts like a waiter who cleans up after you. It ensures that no leftover resources (like open files or database connections) are hanging around unnecessarily.


๐Ÿ“ Example 1: Using a Destructor

Let’s create a Person class that automatically says goodbye when the object is destroyed.

<?php
class Person {
    public $name;

    function __construct($name) {
        $this->name = $name;
        echo "Hello, $name! ๐Ÿ‘‹ <br>";
    }

    function __destruct() {
        echo "Goodbye, $this->name! ๐Ÿ‘‹ <br>";
    }
}

// Creating an object
$person1 = new Person("Alice");
?>

Try It Now

When the script ends or the object is no longer needed, the destructor automatically runs and prints a goodbye message.


๐Ÿ”น How Does PHP Automatically Call Destructors?

  • The destructor is automatically called when the object is unset or the script ends.
  • It is useful for cleaning up database connections, closing files, or releasing memory.

๐Ÿ“ Example 2: Destructor for Closing Database Connections

Imagine we open a database connection inside a class. The destructor ensures it gets closed properly.

<?php
class Database {
    public function __construct() {
        echo "Database connection established. โœ… <br>";
    }

    public function __destruct() {
        echo "Database connection closed. โŒ <br>";
    }
}

// Creating an object
$db = new Database();

// At the end of the script, the destructor runs automatically.
?>

Try It Now

Even if we forget to close the connection manually, the destructor takes care of it.


๐Ÿ“ Example 3: Manually Destroying an Object

We can also manually trigger the destructor by using unset().

<?php
class FileHandler {
    public $filename;

    function __construct($filename) {
        $this->filename = $filename;
        echo "Opening file: $filename ๐Ÿ“‚ <br>";
    }

    function __destruct() {
        echo "Closing file: $this->filename ๐Ÿ“‚ <br>";
    }
}

// Creating an object
$file = new FileHandler("document.txt");

// Manually destroying the object
unset($file);

echo "Script continues... ๐Ÿš€";
?>

Try It Now

The destructor runs immediately after calling unset($file), even though the script is still running.


๐Ÿ”น Using Destructors in Inheritance

Child classes can also define their own destructors.

๐Ÿ“ Example 4: Destructor in an Inherited Class

<?php
class ParentClass {
    function __destruct() {
        echo "Parent class destructor called. ๐Ÿ  <br>";
    }
}

class ChildClass extends ParentClass {
    function __destruct() {
        echo "Child class destructor called. ๐Ÿ‘ถ <br>";
        parent::__destruct(); // Call parent destructor
    }
}

// Creating an object
$child = new ChildClass();
?>

Try It Now

The destructor of the child class runs first, followed by the destructor of the parent class.


๐ŸŽฏ Key Takeaways

  • __destruct() is a special method that runs when an object is destroyed.
  • It helps clean up resources like open files, database connections, or memory.
  • It is called automatically at the end of the script or when an object is unset.
  • Child classes can override destructors but should call the parent destructor when needed.

๐Ÿ“ Practice Time!

Try modifying the examples to create your own Game ๐ŸŽฎ or MusicPlayer ๐ŸŽต class with destructors. Have fun experimenting! ๐Ÿš€