That makes the variable exist only for the duration of that method call.

For instance, you have a CFC which is persisted in the application
scope. 
 
<cfset application.my_cfc = CreateObject('component','CFC.foo')>

It has a method called helloWorld() which queries their name out of the
database a returns it.

<cfquery name="qry_your_name">
Select etc.
</cfquery>

<cfreturn qry_your_name.full_name>

Now, let's say two different people in the same app call that method at
the same time.  Right now, qry_your_name will exist in variables, which
is global to the entire CFC.  

Here we just created a race condition.  User one's query runs, and then
user two's query overwrites.  Then the cfreturn hands back the same data
to both users.

The solution:

<cfset var qry_your_name = "">

<cfquery name="qry_your_name">
Select etc.
</cfquery>

<cfreturn qry_your_name.full_name>

Now EACH call to that method has its own qry_your_name variable which
can not be overwritten by concurrent usage of the CFC

Print this out and place it on your cube wall.  I did.  :)
http://www.coldfusionjedi.com/downloads/cfcscopes.pdf

~Brad

-----Original Message-----
From: Chad Gray [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 30, 2007 8:46 AM
To: CF-Talk
Subject: CFC defining vars

I have noticed in a lot of people's code for CFCs they set all of the
variables being used in the CFC like this:

<cfset var foo = "">

Why?  Is this just good programming practice?

Also what scope are these variables in?  variables.foo?




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
ColdFusion is delivering applications solutions at at top companies 
around the world in government.  Find out how and where now
http://www.adobe.com/cfusion/showcase/index.cfm?event=finder&productID=1522&loc=en_us

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:284804
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to