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?

Jeromy French
  • 11,372
  • 13
  • 67
  • 119
Soldarnal
  • 6,948
  • 9
  • 44
  • 65

4 Answers4

29

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.

Patrick McElhaney
  • 52,844
  • 37
  • 123
  • 157
  • 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 affected – Richard Tingle Dec 10 '14 at 09:12
  • Like Java, ColdFusion doesn't have pointers, so the definitions of "by reference" and "by value" are simplified. – Patrick McElhaney Dec 10 '14 at 21:52
  • 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; http://stackoverflow.com/q/40480/2187042 – Richard Tingle Dec 10 '14 at 22:07
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.

Dave DuPlantis
  • 6,054
  • 2
  • 25
  • 29
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.

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.

Jamie Krug
  • 469
  • 3
  • 5