Now we all know why we should var our variables in CFC methods. If you don't, the variables you create will bleed into other functions, and can wreak havoc on your application, especially if you cache your CFCs.

Suppose you have a component called MyComponent.cfc.

view plain print about
1<component name="MyComponent">
2 <cffunction name="method1">
3 <cfset var myQry="">
4 <cfquery name="myQry" datasource="myDSN">
5 insert into table (somefield) values (1)
6 </cfquery>
7 <cfquery name="myQry" datasource="myDSN">
8 select * from table where somefield=1
9 </cfquery>
10 </cffunction>
11 <cffunction name="method2">
12 <cfdump var="#myQry#">
13 </cffunction>
14</component>

Now lets say you have a test file that looks something like this:

view plain print about
1<cfobject name="application.myComponent" component="myComponent">
2<cfinvoke component="#application.myComponent#" method="method1">
3<cfinvoke component="#application myComponent#" method="method2">

What do you expect the output to be? It should just throw an error when you call method2, saying that myQry is not defined. What will happen is you will see the dump of myQry, which has bled out from the other method.

Apparently, what happens is when the first query runs, it doesn't return anything, and the myQry variable gets erased. (If you try dumping it right afterwards, you will see that it's not defined).

When the second query runs, it will create the myQry variable in the standard scope, which is local to the CFC. There are workarounds, but hopefully Adobe will fix it in their next updater.

One such workaround is to use a struct for the local variables, like this:

view plain print about
1<component name="MyComponent">
2 <cffunction name="method1">
3 <cfset var local=StructNew()>
4 <cfquery name="local.myQry" datasource="myDSN">
5 insert into table (somefield) values (1)
6 </cfquery>
7 <cfquery name="local.myQry" datasource="myDSN">
8 select * from table where somefield=1
9 </cfquery>
10 </cffunction>
11 <cffunction name="method2">
12 <cfdump var="#local.myQry#">
13 </cffunction>
14</component>

Now if you try running the same code, you will get an error saying that "local.myQry" is not defined.