From the course: PHP: Object-Oriented Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Clone method

Clone method

- [Tutor] 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. In 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 shirt1. Then I'm going to set its property color equal to blue. Now, I'm calling clone on shirt1. 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 sign that to shirt2. Now, to begin with, they're perfect duplicates. I haven't changed anything. So if I ask shirt1 for its color, it's blue. And if I ask shirt2 for its color, it's also blue. If I change the color for shirt2, though, it changes to green while shirt1 stays blue…

Contents