Private variables in CFCs

Submitted by Falken on
My least favorite bit of ColdFusion objects (CFCs) came up again toay on CF-Talk:
<cfset var foo = "bar">

<cfset foo = "foo">

These two function-local variables may or may not be the same thing, and the latter may or may not get confused (randomly change value or vanish) without the former.The chance of this happening increases the more concurrent calls to the CFC there are.

If you have un-scoped variables in a CFC method you must make sure they are all var'ed at the start of it.
Note this applies to all variables such as the one cfhttp uses to report ack, query names etc. etc. as well as your own.

Adobe claim some peoples code relies on this broken behaviour and won't fix it. Hopefully CF8 will have a 'auto var' or 'fail to compile if any un-vared' option in the administrator, though I don't hold out much hope.

The canonical reference is http://ray.camdenfamily.com/downloads/cfcscopes.pdf.

Sections