Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sometimes, you will want to make a copy of an object.
There are many reasons to do this, including making a backup before performing an
operation, or as a shortcut for defining a similar object.
To create a copy of an object, use the clone keyword. As a demonstration, make a
copy of the park address, open the demo script, and navigate to the bottom. Add
the following lines:
Cloning AddressPark $address_park_clone = clone $address_park. And
then, we'll debug the clone.
To check the differences between clones, I'm going to compare two objects. Comparing objects is a lot like comparing primitives, such as strings and floats. I can use the comparison operator, double equals (==), which will check to see if the properties are the same. If I need to be more strict, I can use the identity operator, triple equals (===), to also check if both objects are instances of the same class. At the end of the script, add echo $ address_park_clone_ is. And then, a ternary statement. $address_park == $address_park_clone ? : not ] a copy of $address_park. Save the demo, and refresh the browser.
Visually, every property is the same, and the final line indicates this. However, the time created isn't necessarily the same, especially if one object is based on an old record from the database. To deal with this, I'm going to use the _ clone() magic method in the address class. If the method exists, it will be called after object cloning. As an example, I am going to use this to reset the time created. Let's go back to the address class. Then, after the properties, add the clone method, which takes no arguments. Remember to add proper documentation.
For the demonstration, I will explicitly set the time_created and time_updated to obvious values. Save, then switch to the browser, and refresh. The time_updated and created have been set to the new values, and the final line states that $address_park_clone is not a copy of $address_park. Return to the clone function, and set time_ created = time, and time_updated = Null. Now that I've demonstrated object cloning, I am going to discuss how to reference objects.
Referencing can be similar to cloning, but there are enough differences to make it worth exploring on its own.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101694 Viewers
61 Video lessons · 88445 Viewers
71 Video lessons · 72283 Viewers
56 Video lessons · 104006 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.