DEV Community

Cover image for PHP __destruct (🧙‍♂️Lesson 2: PHP Magic Methods)
Clean Code Studio
Clean Code Studio

Posted on • Updated on

PHP __destruct (🧙‍♂️Lesson 2: PHP Magic Methods)

Twitter Follow


PHP __destruct Magic Method method

During Lesson One of our Php Magic Methods Tutorial we learned about the infamous __construct Php Magic Method.

What's the opposite of construction? Destruction!

Dive in with me as we learn how to property utilize the PHP destruct magic method in the second lesson of our PHP Magic Methods series.


To use the destruct magic method we simply add the __destruct magic method as a public function on our class.

In the example below our constructor accepts the $lady and $gentleman in our constructor. The constructor is called when our object in instantiated or constructed.

The destructor magic method on the other hand is the exact opposite. The __destruct PHP magic method is going to be executed when the object is destroyed (using unset as shown below) or when the PHP run time is complete.

<?php

class Home
{
    public $lady;
    public $gentleman;

    public function __construct($lady, $gentleman)
    {
        $this->lady = $lady;
        $this->gentleman = $gentleman;

        echo "Welcome Home {$this->lady} & {$this->gentleman}!\n\n";
    }

    public function __destruct()
    {
        echo "You'll about to die {$this->lady} & {$this->gentleman}...guess it was a simulation after all \n";
    }
}

$home = new Home('Sarah', 'Tom');
$lake_house = new Home('Spicy Senorita', 'Surfer Cali Dude');

unset($home); // destroying or obliterating

// application ends and then $lake_house destructor will execute
Enter fullscreen mode Exit fullscreen mode

Output

"Welcome Home Sarah & Tom"

You'll about to die Sarah and Tom...guess it was a simulation after all.
Enter fullscreen mode Exit fullscreen mode

What if we use the sleep function to pause our runtime for 5 seconds before we call the unset function to destroy our object?

<?php

class Home
{
    public $lady;
    public $gentleman;

    public function __construct($lady, $gentleman)
    {
        $this->lady = $lady;
        $this->gentleman = $gentleman;

        echo "Welcome Home {$this->lady} & {$this->gentleman}!\n\n";
    }

    public function __destruct()
    {
        echo "You'll about to die {$this->lady} & {$this->gentleman}...guess it was a simulation after all \n";
    }
}

$home = new Home('Sarah', 'Tom');
$lake_house = new Home('Spicy Senorita', 'Surfer Cali Dude');

sleep(5);

unset($home); // destroying or obliterating

// application ends and then $lake_house destructor will execute
Enter fullscreen mode Exit fullscreen mode

Output

Welcome Home Sarah and Tom
Enter fullscreen mode Exit fullscreen mode

5 second pause

Output

You'll about die Sarah and Tom...guess it was a simulation after all
Enter fullscreen mode Exit fullscreen mode

If we removed the unset function all together, then the __destruct magic method is run when the PHP runtime is complete.


PHP __destruct magic method

Top comments (1)

Collapse
 
cleancodestudio profile image
Clean Code Studio