DELAPARRA
Index
Color
NL
Category: Programming paradigms

Clone

Explained by Updated

What is Clone?

Clone: Creating a new object by copying an existing one, rather than instantiating from a class definition. Cloning creates a new object by copying an existing one instead of building it from scratch. In PHP you use the clone keyword; in JavaScript structuredClone() or the spread syntax for simple objects. The important detail is shallow versus deep copying. A shallow clone copies the object itself, but nested objects are still shared. Change a nested address on the copy and the original changes too. A deep clone copies everything.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Clone

What should you keep in mind with Clone?

In PHP you control this with the __clone() method, where you clone nested objects explicitly. Cloning is useful for templates, such as a default configuration you adjust per use, and in tests. Immutable objects, which return a new copy whenever something changes, avoid most of these surprises and are often the cleaner design.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with C

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.