Join Kevin Skoglund for an in-depth discussion in this video Destructor method, part of PHP: Object-Oriented Programming.
- [Instructor] Now that we've learned about the constructor method, let's talk about its companion, the destructor method. The way that we define the destructor method is similar to what we did with the constructor method. Inside our class, we define a new method, whose name is __destruct(). We want to make sure that that's a public method, so we mark it with public at the beginning, and unlike construct, destruct takes no argument. Inside the construct method defintion, you could put any code that you want to be called whenever an instance is destroyed.
This is most useful for performing logging or other cleanup actions. It can be used together with construct as a set of setup and teardown methods, although in truth, you're going to use construct much more frequently than you will destruct, and the reason why is just the nature of how we use objects in PHP. Most of the time we instantiate a new object, we work with it throughout the script, and then at the end, we return the result back to the user, and we don't worry about what happens to those objects, and we just let PHP deal with the left over objects on its own.
So the destructor method is going to be called whenever the last reference to an instance is removed. So what it means by reference is the variable that we create that points to the new instance that we've created. We create an instance, we assign it to the variable, that's the reference to the instance. So if we remove that reference, then it will remove the object. PHP will say, "Well, nothing refers to this object anymore, "so it's no good. I can throw it away." And it'll call that destruct method. So there are two ways that we can do that.
The first is that we can unset the reference. So we call unset on the reference that points to some instance, now there's nothing referring to it anymore, so PHP throws it away. And the other way to do it is when the PHP script exits. We'll talk more about that in a moment. First, let's look at unset. Here I had the exact same definition for the class Product. You'll see that I'm instantiating a new Product and assigning it to the variable shirt. Now, shirt is a reference to that instance. I can then ask for the instance_count static property, and it returns one because I incremented the value.
Then I call unset on that variable, now the variable's not set to anything anymore. So that instance has been orphaned. Nothing refers to that instance anymore, PHP sees it as trash, it calls the destruct method on it and gets rid of it. So now, instance_count has been decremented because that destruct method was called, and it hit that piece of code that decremented the number. Now as I mentioned, it also takes place whenever the script exits, and we have to be very careful about that, because that can create some unintended consequences. Let's do public function __destruct(), there's no arguments, and inside of here, before we do anything else, let's just do echo and let's just say "BYE!".
All right, so that's all it's going to do. Whenever an instance is destroyed, we should see the word bye. So let's go back over to our page and let's load it up. And let's hit reload. Look at that. Bye, bye, bye. Three times. Why is that? Well I have three different instances. One of Sofa, one of Couch, and one of Loveseat. Come down here. You can see I have new three times. So I created three instances. I didn't intentionally unset any of them. Instead, I let the script finish running and PHP unset them for me. So be careful about that, it happens automatically.
Destruct gets called at the end of your PHP script. I could also do it on purpose here. Let's come down here before we get to Loveseat, and let's do unset($sofa), right? We don't need that instance anymore when we get down here. I'll save it. Let's come back and reload the page. So now bye appears in front of Loveseat. It happens right when I unset it. These other two are being destroyed when the script ends. Okay, so now that we understand when it gets called, now let's actually put in something useful here. Let's just take it and decrement the count.
Instead of doing any kind of echoing, we're just going to say instance_count--. So now, let's save it. Let's go back and let's reload the page. Now my instance count is two. Why is that? Well, if we come down here and look, it unsets one of them here, then it echos the instance count, and then the other two get unset by PHP automatically at the end. While I do think it's important for everyone to understand what the destructor method is and what it does and how it works, the reality is, you're going to use it in your projects very rarely.
I think you'll find it's most useful when you want to perform logging and cleanup operations.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project