NO MORE CAREER
POLITICIANS!
Get Out Of Our House: Replacing congress with TRUE citizens!
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Web Hosting

best web hosting - top web hosting sites, thetop10bestwebhosting.com

Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< November, 2008 >>
SMTWTFS
1
2345678
9101112131415
16171819202122
23242526272829
30
Search Blog

Recent Comments
Re: Railo 3.1 on Windows Server 2008 and IIS7 - Part 3 of 3 (by Jon at 8/27 2:04 PM)
Re: Hosts File Changes Not Acknowledged on Vista 64 (by Spacy at 8/24 3:46 PM)
Re: THE DAY CFUNITED DIED (by ComboFusion at 8/23 10:50 AM)
Re: My Grandpa (by Tasha at 8/10 4:29 PM)
Re: Just What IS a 'Service Layer', Anyway? (by dougboude at 8/02 10:10 AM)
Re: Just What IS a 'Service Layer', Anyway? (by Isaac at 8/02 2:25 AM)
Re: PayPal IPN Coldfusion CFC (by Soyestudiambre at 7/25 6:12 PM)
Re: PHP vs COLDFUSION (by Tony Garcia at 7/17 11:24 AM)
Re: PHP vs COLDFUSION (by dougboude at 7/14 8:45 AM)
Re: PHP vs COLDFUSION (by Lola LB at 7/14 5:51 AM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

16 June 2006
ColdFusion Component Variable Scopes: This, Variables, and Var
Scope within a CFC is incredibly important, and even moreso when you start using Object Oriented Frameworks for your application. It is essential to understand it in order to save yourself a lot of mind numbing troubleshooting, especially when writing recursive functions.

The following information is also contained in my OO Lexicon, but I felt like it was sufficiently encapsulated (that's an OO term ) to go ahead and re-post it here.

THIS, VARIABLES, and VAR Scopes

These are three of the variable scopes that are found within the world of a ColdFusion object or component, and three scopes that can make you pull the rest of your hair out when you don’t know how to think about them. In a nutshell, you’re looking at three increasing levels of variable privacy, from most liberal to most private.

The “THIS” scope holds items that can be directly accessed from anywhere inside OR outside of the object itself. Consider the following example of an instantiation of the myTest.cfc that has a variable called THIS.GLOBALVAL within it’s INIT method:

The component code...

<cfcomponent>
       <cfset this.GLOBALVAL = "Whatchoo lookin at Willis?" >
       <cffunction name="init" output="false" returntype="myTest">
          <cfreturn THIS>
       </cffunction>
    ...
</cfcomponent>

The call from the outside template...

 

 

<cfscript>

            myTestObj = CreateObject(“component”,”myTest”).Init();

            myTestObj.GlobalVal = “I set you from outside of the object!”;

</cfscript>

 

In this example, because the variable GlobalVal was put into the THIS scope within our object, our application could directly access it as a property. Cool, if that’s what you intended to happen. Not cool if it wasn’t.

 

 

 

The VARIABLES scope within a component object is a scope that can be accessed by any method within the object at any time, in real time. In other words, if our component had set up a variable called Variables.LimitedVal, all methods will be sharing that one instance of the variable. If method one sets it to “5”, and later the app calls method two which reads that variable, it will see the value “5”. Any attempt, however, from outside the object itself to manipulate that value will result in an error. The following would FAIL:

The component code...

<cfcomponent>
       <cfset variables.GLOBALVAL = "Whatchoo lookin at Willis?" >
       <cffunction name="init" output="false" returntype="myTest">
          <cfreturn THIS>
       </cffunction>
    ...
</cfcomponent>

The call from the outside template...

 

 

<cfscript>

            myTestObj = CreateObject(“component”,”myTest”).Init();

            myTestObj.GLOBALVAL = “I set you from outside of the object!”;

</cfscript>

 

 

 

 

And finally, the VAR scope. This scope is one which can be seen only from inside of the actual method itself. For example, I can have three methods, each that use a variable with the same name that was initialized within the VAR scope, and no method will ever see the variable used by the other methods. It is a VITAL thing that you initialize your private variables in the VAR scope inside of your methods, because by default they are set up in the VARIABLES scope, and who knows WHAT havoc will occur if you have methods sharing variables that were intended to be private. Consider the following sample of initializing a variable in the VAR scope:

 

<cffunction access="public" name="sampleMethod" output="false" returntype="void">

                        <cfargument name="headlines" type="array" required="yes" >

                        <cfargument name="sourceID" type="numeric" required="yes">

                       

                        <cfset var iterations = 0>

                        <cfset var urlitems = "">

                        <cfset var newInsertItems = arraynew(1)>

</cffunction>

 

Only the ‘sampleMethod’ method will be able to see and manipulate those variables set using the ‘var’ scope. VAR is so private, that every other method within our component could initialize variables with the same name within their own var scope and no overlap would occur.




Posted by dougboude at 12:00 AM | PRINT THIS POST! |Link | 8 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
this is easy enough to understand by even the CF challenged. I'd personally like to see more of this. Thanks!
Posted by macwebdiva on June 18, 2006 at 11:36 AM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
Var is not a scope but is instead a keyword added to a CFSET to cause the variable being created to exist only within a special Variables scope. This Variables scope is the global variables plus the variables only available to the function. I.E the local variables.
Posted by Michael Dinowitz on August 25, 2006 at 1:23 AM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
Very helpful post. You really just demystified this for me. Thanks Doug!!
Posted by Fitz on October 9, 2007 at 10:01 AM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
My pleasure, Fritz! I believe that everybody should share what they know...it's the spirit of the net.
Posted by dougboude on October 9, 2007 at 10:31 AM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
Thanks, this is a perfect example. Clear and detailed.
Posted by none on May 1, 2008 at 8:49 AM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
"It is a VITAL thing that you initialize your private variables in the VAR scope inside of your methods" and "who knows WHAT havoc will occur"

- What havoc = if you forget to VAR your CFSETs AND store your queries (yes these are objects with scope also!) these will stay in server memory for that thread until they are dumped or the server goes down. For queries, I've used storage structs and then attach a query as part of that struct (so it gets destroyed at end of the function execution): and
Posted by DCDavis on January 14, 2009 at 6:44 PM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
Dude...er, uh, Dieter..

The havoc comes when you have fifty different users in all parts of the world hitting the same CFC at the same time, and in a nutshell, "their data gets crossed". You could potentially have user A login first, user B logs in second, then user A starts seeing user B's data. I'd call that havoc. I'm sure the rest of the CFers out there could come up with a whole book full of ugly scenarios that could rightly be called havoc, too, just because someone did not scope their variables properly.
Posted by dougboude on January 15, 2009 at 8:33 AM

Re: ColdFusion Component Variable Scopes: This, Variables, and Var
good example! It explained clearly the behaviour of all the three variables.
Posted by bhavani on June 3, 2010 at 3:55 AM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

6 plus 2 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!