 Hey everybody, this is Brian and welcome to the 36 lamp tutorial. Today we're going to be discussing constructors and destructors. Class, we'll call this my class. Now we know what functions are, and we know that we can create classes and destroy them. I haven't really talked about destroying them yet, but they are destroyed. So how do we actually know what to do when they're created and destroyed? Well, if you go out to the PHP website and you look this up, you see how there's special functions called a construct and a destruct. Now, they might be a dead giveaway, but construct happens when the class is constructed. And destruct happens when the class is destructed. Now notice this double underscore in the front here. That's a special thing you got to really understand. So we can say function. Notice how there's a whole bunch of these, but we're just going to focus on construct. And then we'll make one for destruct. And then we're just going to echo out some stuff here. Just going to say I'm alive. And then what's a good one? I'm melting. There we go. I had that movie stuck in my head the other day. Then we're just going to make a new instance of our class. So we'll say what's called my C equal new, my class. Save and run. Now notice we haven't actually done anything, but automatically we get I'm alive and I'm melting. So what's going on here? Well, in the PHP page lifecycle, if you will, because the browser does not have a keep alive, it just says date to load, unload. What we're doing is we're creating an instance of this class, then we do something. And right here, or actually, I should say down at the end of this down here, PHP will actually unload everything from memory, meaning whatever's not being used gets destroyed. So let's talk about this for just a second. You have a constructor, which is an automatic function. It's automatically called by PHP. And you have a destructor or destruct, which is automatically called by PHP. Notice how we didn't do a thing, yet our code was executed. Now why would you want to do something like this? Now let's say you were connecting to a database. I know we haven't really gotten into databases yet, but we'll just say, let's see here, public is alive, equal false. Now what you could do is you say true and false. Pretty shoddy example. I said the word shoddy, by the way. My daughter says that a lot shoddy, and what's the other one snarky. I never heard that before snarky. Is that even a word? Anyways, but basically you can see how you can turn things on, turn things off. So if you had a persistent database connection when the class is created, you could automatically initialize it and when it's destroyed, you could destroy that connection in the background. A lot of things are done automatically for you, but if you really want fine to control, you can do this. Going forward, you may never use construct and destruct, but you should know that they exist and why they exist. That's all for this tutorial. I hope you found this educational and entertaining, and thank you for watching. Thank you.