Join Kevin Skoglund for an in-depth discussion in this video Clone method, part of PHP: Object-Oriented Programming.
- [Narrator] In this movie, we're going to talk about the Clone Magic Method. Before we learn about the Magic Method Clone, we first should learn a keyword, which is also clone, and it works a lot like the way we use new. This example, you can see that I'm creating a new instance of the product class, and I'm assigning that instance to the variable shirt one. Then I'm going to set its property color equal to blue. Now I'm calling clone on shirt one, and it's going to create a duplicate of that instance. It's a new instance.
It's a complete duplicate, and it's going to duplicate all the property values that are in the original one. I'm going to assign that to shirt two. Now to begin with, they're perfect duplicates. I haven't changed anything. So if I asked shirt one for its color, it's blue, and if I ask shirt two for its color, it's also blue. If I change the color for shirt two though, it changes to green, while shirt one stays blue, because they are two separate instances. It's exactly like if I created a brand new instance, and then I set all of its properties to be exactly the same as the first instance.
Clone just does it for us in one simple step. It can be really handy. Okay, so now that we know about that clone keyword, now we can talk about the Clone Magic Method, because where construct is called magically, whenever we call new, the Clone Magic Method is called whenever we call clone. The way we define the Clone Magic Method, is similar to what we had for construct and destruct. You're going to define the method inside your class. You want to use a double underscore before the name, and you must also set the visibility of the method to be public.
Unlike construct, clone won't take any arguments. It's just simply going to take the existing instance and clone that, but it's going to run the code that we have inside the method first. Now cloning does not call the constructor method, just clone. So there are two separate parallel structures. One is when we're creating a new instance, and one is when we're duplicating an instance. So in our code where we're adding and subtracting from the property instance count, we must consider that that an instance could be created by cloning.
If we didn't consider it, a new instance would be created, but the construct method would not be called, and the count would not increase by one. So here you can see that I've added a line of code so that it increments that value even when clone is called. So it doesn't matter if I'm creating a new instance, or I'm cloning an instance, I'm going to add one to the instance count. Besides keeping track of the instance count, the other way that clone is most useful is to think about the way that an object should be copied. When we're copying an instance, do we want to do some other housekeeping, before we're done with that copy? In the clone method, we have the ability to modify the instance or to execute additional behaviors while it's being cloned.
So let's try this out in our sandbox, and let's do that by creating a new file, and I'm going to call this cloning dot php, and in here, inside our php tags, I'm going to make a new class for beverage, and beverage is just going to have one property. It's going to be public, and it's going to be name. And let's set function, underscore, underscore, construct. We know how to do that, and we'll just echo back new beverage was created, okay? So it's just going to get that information.
I'm going to copy that now, and I'm going to make another version of it. Oops, I misspelled that construct, and then I'm going to change this one to be clone. Instead of new beverage, I'm going to make it existing beverage was copied. Okay? So new beverage was created, existing beverage was copied. That's going to tell me which one is being called. Alright, so now let's try it, so we see each one. So let's do A equals new beverage. Now let's set A name equal to coffee.
This will help us tell different instances apart if they have different names, and let's echo back that name, dollar sign, A, name, and let's put a BR tag at the end. Okay, so let's just try that much so far. Let's come back over here, and instead of constructors, cloning. So new beverage was created, and coffee, right? We haven't called clone yet. That's exactly what we would expect. So now, let's clone something.
We're going to clone dollar sign A. Right? I'm passing in the instance. I'm not passing in the class name, it's the instance. I want to copy this instance, and I want to copy its properties. So let's just copy this line. I'll come down here, and let's do echo name for each one. We're going to look at what the name is for A, and what the name is for for B, and I'll just also add a line here, with an HR tag, so we can of keep them all separate. Alright, let's come over here and let's reload our page.
Existing beverage was copied. See how it happens? It gets cloned. We call that Magic Method. That's when existing beverage get copied pops up, and now it tells us the values are both coffee, because it copied the value of the property for name at the same time. But they are in fact different values. The name going to be equal to, let's make it T. Alright, and then we'll copy these. Put them down here. Let's also copy that HR tag, and just kind of keep them all separate.
Alright, let's reload our page. So we create it. Here's the first one created, it's coffee. We make a duplicate. Now we have coffee and coffee, and then I can change the value of the second one to be T, but it has no effect on the first one, because it is a separate instance. Notice when I cloned it, it did not call new beverage was created. It only called the code that was inside clone, so it depends which one we call. If we're calling new, we get the construct method. If we call clone, we get the clone method.
- 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