Are ColdFusion objects passed by reference or by value?
Asked Answered
B

4

26

Are ColdFusion objects (i.e. CFCs invoked via cfobject) normally passed by reference or by value (copied in memory)? And is it possible to force ColdFusion to pass an object in either manner?

Bandsman answered 20/10, 2008 at 19:27 Comment(0)
M
30

ColdFusion objects are passed by reference. There's not really a way to pass an object by value, but you can create a copy using the duplicate() function.

Millenarian answered 20/10, 2008 at 19:44 Comment(3)
My understanding was that just like java cold fusion passed a reference by value. This is subtly different to passing by reference because if a function is passes an object as argument in and then in is changed within the function to a completely different object that does not affect the variable passed to the function. Whereas in C where you truly can pass by reference the passed variable would be affectedMerill
Like Java, ColdFusion doesn't have pointers, so the definitions of "by reference" and "by value" are simplified.Millenarian
While this is true (and so there is no true pass-by-reference, thankfully) the difference between pass-by-reference and pass-a-reference-by-value is still important. This question regarding java does a very good job of explaining the issue; https://mcmap.net/q/18334/-is-java-quot-pass-by-reference-quot-or-quot-pass-by-value-quot/2187042Merill
A
15

Well, keep in mind that using duplicate() doesn't work in MX 7 for copying certain things, like CFCs and COM, CORBA, and Java objects. You can copy a CFC in CF 8, though.

Abernathy answered 20/10, 2008 at 20:10 Comment(0)
M
10

Complex objects such as structs and CFCs are passed by reference, but if you pass an array to a UDF it is passed by value. This makes it impossible to write a user defined function that mutates an array like the built-in array functions do. If you really need to pass an array by reference, you can wrap it in a struct, and then it will be passed by reference.

Madeleinemadelena answered 10/12, 2008 at 20:55 Comment(0)
W
4

FWIW, arrays are also passed by reference in the Railo CFML engine, as many developers believe they should. Railo 3.1 is also expected to offer an administrative setting to allow you to choose the default behavior, for optional compatibility with Adobe CF.

Waftage answered 26/6, 2009 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.