Cloning Objects in PHP?
I keep reading about cloning objects in PHP but none of the articles and sections I’ve read explain why you would want to clone an object.
Why would someone want to clone an object?
Observing members:
0
Composing members:
0
2 Answers
I usually use it when I’m at a midpoint that I’ll be using for several objects.
How about a oversimplified example just to illustrate the point?
If I’m programming a Christmas story and have all my characters as objects (bear with me here)... I might start with an “animal” class for the reindeer, set up the specific properties that make it a reindeer (legs = 4, horns = true, etc), and then clone it for each of Santa’s reindeer before setting the particulars like the specific name (Rudolph, Blitzen, etc)...
The alternative is to start over from my animal class, or from scratch, and loop through to set the same properties. Cloning would be more efficient, and usually easier to follow.
In the real world I might use this for something like an XML object. I’ll start with the base, set the particulars of a formatted request that I’ll be reusing, and then clone the XML object before filling in the particulars.
Thanks Funkdaddy (lol) I have a better understanding of it, but it made me realize I don’t understand OOP enough to fully understand it.
Time to do some more reading!
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.