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

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 p…


This content originally appeared on DEV Community and was authored by Clean Code Studio

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

Output

"Welcome Home Sarah & Tom"

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

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

Output

Welcome Home Sarah and Tom

5 second pause

Output

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

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

.ltag__user__id__386677 .follow-action-button { background-color: #339d15 !important; color: #ffffff !important; border-color: #339d15 !important; }
cleancodestudio image

PHP __destruct magic method


This content originally appeared on DEV Community and was authored by Clean Code Studio


Print Share Comment Cite Upload Translate Updates
APA

Clean Code Studio | Sciencx (2021-07-29T14:25:08+00:00) PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods). Retrieved from https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/

MLA
" » PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods)." Clean Code Studio | Sciencx - Thursday July 29, 2021, https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/
HARVARD
Clean Code Studio | Sciencx Thursday July 29, 2021 » PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods)., viewed ,<https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/>
VANCOUVER
Clean Code Studio | Sciencx - » PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/
CHICAGO
" » PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods)." Clean Code Studio | Sciencx - Accessed . https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/
IEEE
" » PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods)." Clean Code Studio | Sciencx [Online]. Available: https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/. [Accessed: ]
rf:citation
» PHP __destruct (?‍♂️Lesson 2: PHP Magic Methods) | Clean Code Studio | Sciencx | https://www.scien.cx/2021/07/29/php-__destruct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-2-php-magic-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.