Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 29, 2021 02:25 pm GMT

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 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.

<?phpclass Home{    public $lady;    public $gentleman;    public function __construct($lady, $gentleman)    {        $this->lady = $lady;        $this->gentleman = $gentleman;        echo "Welcome Home {$this->lady} & {$this->gentleman}!

"; } public function __destruct() { echo "You'll about to die {$this->lady} & {$this->gentleman}...guess it was a simulation after all
"; }}$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?

<?phpclass Home{    public $lady;    public $gentleman;    public function __construct($lady, $gentleman)    {        $this->lady = $lady;        $this->gentleman = $gentleman;        echo "Welcome Home {$this->lady} & {$this->gentleman}!

"; } public function __destruct() { echo "You'll about to die {$this->lady} & {$this->gentleman}...guess it was a simulation after all
"; }}$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


Original Link: https://dev.to/cleancodestudio/php-destuct-lesson-2-php-magic-methods-111i

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To