Destructor

Ultimate PHP and MySQL Web Development Object Oriented Programming
2 minutes
Share the link to this page
Copied
  Completed
A destructor is a special member function that is called when the lifetime of an object ends. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.

Transcript

Hello, and this video we are going to cover the D structure. So if you haven't watched the constructor video, I highly recommend that you do that. Because he explains, you know what constructor is which is quick overview for low function that is run at the start of when you create a object of the class, like we did here, the destructor is just the opposite. It runs at the end of the class Am I thinking when was the end? So let me show you. The format is probably shown on the smaller score.

D open close parentheses, curly braces, and in here we'll just say dead. So he says I am dead. I am dead. We didn't specifically call the destruct method. You know because obviously this is called when you create it. This is cold when the phone I mean the object no longer exists and In this case, when it gets to the end of our website, or you know, at the end of our class or at the end of our file, and it could also be to do with scope as well.

So if the object of the class was created within a function, for example, then the end of the objects life would be when he hits the end of the function, which will be the closing curly brace. So as an extra task, I want you to combine not a function inside of here, just another function that when called, you just create an object of this class, and then you will see, because once it hits the handler function, you cannot access that class that was created, or the object that was created within that function outside it is now destroyed. So why would you want to do this, you probably want to want to print something out. But here, you can handle some stuff. So you could handle some memory management. You could potentially store some statistics, maybe at the end You want to store statistics to some database.

So something that you want to do at the end, this is great, you know, for doing the at the end of the lifecycle of an object of a class. And yeah, that's it. That's all you need to know for destructor. If you have any questions, as usual, you know, put me a message and I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.