Menu
Coddy logo textTech

Destructor (__destruct)

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 8 of 91.

The __destruct method is a special method that is automatically called when an object is destroyed. An object is destroyed when there are no more references to it or when the script ends.

Here is an example of a class with both a constructor and a destructor:

<?php
class Resource {
    public $name;

    public function __construct($name) {
        $this->name = $name;
        echo $this->name . " was created\n";
    }

    public function __destruct() {
        echo $this->name . " was destroyed\n";
    }
}

Create an object and watch the lifecycle:

echo "=== Start ===\n";
$r = new Resource("Connection");
echo "Using " . $r->name . "\n";
unset($r);  // explicitly destroy the object
echo "=== End ===\n";

Output:

=== Start ===
Connection was created
Using Connection
Connection was destroyed
=== End ===

The unset() function removes the reference to the object, triggering the destructor immediately.

If you don't call unset(), the destructor runs automatically when the script finishes:

$r = new Resource("Logger");
echo "Working...\n";
// __destruct is called automatically at end of script

Output:

Logger was created
Working...
Logger was destroyed

Key Point: The __destruct method is useful for cleanup tasks like closing files, releasing resources, or logging when an object is no longer needed.

challenge icon

Challenge

Medium

Complete the Resource class in resource.php:

  1. Add a __construct method that takes $name, sets it as a property, and prints "[name] was created"
  2. Add a logMessage method that takes $message and prints "Log: [message]"
  3. Add a __destruct method that prints "[name] was destroyed"

The driver.php file will test the lifecycle of your Resource objects.

Cheat sheet

The __destruct method is automatically called when an object is destroyed (when there are no more references to it or when the script ends).

Example of a class with constructor and destructor:

<?php
class Resource {
    public $name;

    public function __construct($name) {
        $this->name = $name;
        echo $this->name . " was created\n";
    }

    public function __destruct() {
        echo $this->name . " was destroyed\n";
    }
}

The unset() function removes the reference to an object, triggering the destructor immediately:

$r = new Resource("Connection");
unset($r);  // explicitly destroy the object

If unset() is not called, the destructor runs automatically when the script finishes.

The __destruct method is useful for cleanup tasks like closing files, releasing resources, or logging.

Try it yourself

<?php
require_once 'resource.php';

$testCase = trim(fgets(STDIN));

if ($testCase == "basic_test") {
    echo "=== Start ===\n";
    $r = new Resource("FileHandler");
    echo "Using " . $r->name . "\n";
    unset($r);
    echo "=== End ===\n";
} elseif ($testCase == "auto_destruct_test") {
    $r = new Resource("Logger");
    $r->logMessage("Application started");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming