Doug's Resume
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...

heaters
hotels boeken in 7 sec
Engagement Rings
Online Dating Australia




SURF'S UP!
You:
Your Web Site:
<< May, 2008 >>
SMTWTFS
123
45678910
11121314151617
18192021222324
25262728293031
Search Blog

ColdFusion Jobs
Recent Comments
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by Dan Roberts at 5/15 2:38 PM)
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by Dan Roberts at 5/15 10:06 AM)
Re: Inline CSS and Ajax Issue with IE (by Ben Nadel at 5/14 11:50 AM)
Re: The Perfect Alternative to Gas Powered Vehicles (by Thomas Messier at 5/09 12:47 PM)
Re: Promoting Family Unity: Lowering Your Utility Bills! (by Fernando Lopez at 5/07 10:12 PM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 10:56 AM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/06 10:27 AM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 6:50 AM)
Re: Why I Hate ORMs (a solicited rant) (by Sean Corfield at 5/06 1:40 AM)
Re: Why I Hate ORMs (a solicited rant) (by Steve Bryant at 5/05 5:07 PM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS
Reciprocal Links

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 | 5 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

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!!!

What letter comes four place(s) before the letter N?
Type your answer exactly two time(s) in the designated box.

Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!