User Guide Cancel

Duplicate

 

Note:

As of ColdFusion (2021 release), CORBA has been removed. You can no longer use CORBA-related features, functions, and tags.

Description

Returns a clone, also known as a deep copy, of a variable. There is no reference to the original variable.

Returns

A clone of a variable.

Category

Structure functionsSystem functions

Function syntax

Duplicate(object)
Duplicate(object)
Duplicate(object)

See also

StructCopy, other Structure functionsModifying a ColdFusion XML object in the Developing ColdFusion Applications

History

  • ColdFusion (2025 release): Added an optional parameter, deepCopy.
  • ColdFusion (2018 release): Introduced named parameters.
  • ColdFusion 8: Changed behavior: this function can duplicate CFCs.
  • ColdFusion MX: Changed behavior: this function can be used on XML objects.

Parameters

Parameter

Type

Description

object

 

Name of a variable to duplicate

deepCopy Boolean If deepCopy is TRUE, the child elements are cloned. If deepCopy is FALSE, child elements remain linked to their respective elements in the original object.

Usage

Use this function to duplicate complex structures, such as nested structures and queries.When you duplicate a CFC instance, the entire CFC contents is copied, including the values of the variables in the this scope at the time you call the Duplicate function. Thereafter, the two CFC instances are independent, and changes to one copy, for example by calling one of its functions, have no effect on the other copy.

Note: With this function, you cannot duplicate a COM, CORBA, or JAVA object returned from the cfobject tag or the CreateObject function. If an array element or structure field is a COM, CORBA, or JAVA object, you cannot duplicate the array or structure.

Example

<cfscript>
s1 = StructNew()
s1.nested = StructNew()
s1.nested.item = "original"
copy = StructCopy(s1)
clone = Duplicate(s1)
// modify the original struct
s1.nested.item = "modified"
writeOutput("The copy contains the modified value: " & copy.nested.item & "<br/>")
writeOutput("The duplicate contains the original value: " & clone.nested.item)
</cfscript>
<cfscript> s1 = StructNew() s1.nested = StructNew() s1.nested.item = "original" copy = StructCopy(s1) clone = Duplicate(s1) // modify the original struct s1.nested.item = "modified" writeOutput("The copy contains the modified value: " & copy.nested.item & "<br/>") writeOutput("The duplicate contains the original value: " & clone.nested.item) </cfscript>
<cfscript>
    s1 = StructNew()
    s1.nested = StructNew()
    s1.nested.item = "original"
    copy = StructCopy(s1)
    clone = Duplicate(s1)
    // modify the original struct
    s1.nested.item = "modified"
    writeOutput("The copy contains the modified value: " & copy.nested.item & "<br/>")
    writeOutput("The duplicate contains the original value: " & clone.nested.item)
</cfscript>

Example- deepCopy is TRUE

code

<cfscript>
originalStruct = {
name = "Alice",
address = {
street = "123 Main St",
city = "Wonderland"
},
mobile = 7777777777
}
// Create a deep & a shallow copy
deepCopy = duplicate(originalStruct);
shallowCopy = duplicate(originalStruct, true);
// Modify a nested property in the original struct
originalStruct.address.street = "456 Oak St";
// Display results
writeOutput(#deepCopy.address.street#& "<br/>"); // deep copy
writeOutput(#shallowCopy.address.street#); // shallow copy
</cfscript>
<cfscript> originalStruct = { name = "Alice", address = { street = "123 Main St", city = "Wonderland" }, mobile = 7777777777 } // Create a deep & a shallow copy deepCopy = duplicate(originalStruct); shallowCopy = duplicate(originalStruct, true); // Modify a nested property in the original struct originalStruct.address.street = "456 Oak St"; // Display results writeOutput(#deepCopy.address.street#& "<br/>"); // deep copy writeOutput(#shallowCopy.address.street#); // shallow copy </cfscript>
<cfscript>
    originalStruct = {
        name = "Alice", 
        address = {
            street = "123 Main St", 
            city = "Wonderland"
        }, 
        mobile = 7777777777
    }
    // Create a deep & a shallow copy
    deepCopy = duplicate(originalStruct);
    shallowCopy = duplicate(originalStruct, true);

    // Modify a nested property in the original struct
    originalStruct.address.street = "456 Oak St";
    // Display results
	writeOutput(#deepCopy.address.street#& "<br/>"); // deep copy
	writeOutput(#shallowCopy.address.street#); // shallow copy
</cfscript>

Output

123 Main St
123 Main St

Example- deepCopy is False

code

<cfscript>
originalStruct = {
name = "Alice",
address = {
street = "123 Main St",
city = "Wonderland"
},
mobile = 7777777777
}
// Create a deep & a shallow copy
deepCopy = duplicate(originalStruct);
shallowCopy = duplicate(originalStruct, false);
// Modify a nested property in the original struct
originalStruct.address.street = "456 Oak St";
// Display results
writeOutput(#deepCopy.address.street#& "<br/>"); // deep copy
writeOutput(#shallowCopy.address.street#); // shallow copy
</cfscript>
<cfscript> originalStruct = { name = "Alice", address = { street = "123 Main St", city = "Wonderland" }, mobile = 7777777777 } // Create a deep & a shallow copy deepCopy = duplicate(originalStruct); shallowCopy = duplicate(originalStruct, false); // Modify a nested property in the original struct originalStruct.address.street = "456 Oak St"; // Display results writeOutput(#deepCopy.address.street#& "<br/>"); // deep copy writeOutput(#shallowCopy.address.street#); // shallow copy </cfscript>
<cfscript>
    originalStruct = {
        name = "Alice", 
        address = {
            street = "123 Main St", 
            city = "Wonderland"
        }, 
        mobile = 7777777777
    }
    // Create a deep & a shallow copy
    deepCopy = duplicate(originalStruct);
    shallowCopy = duplicate(originalStruct, false);

    // Modify a nested property in the original struct
    originalStruct.address.street = "456 Oak St";
    // Display results
	writeOutput(#deepCopy.address.street#& "<br/>"); // deep copy
	writeOutput(#shallowCopy.address.street#); // shallow copy
</cfscript>

Output

123 Main St
456 Oak St

Get help faster and easier

New user?