Search

Monday, October 08, 2007

ColdFusion CFC Scope Exploration

Doug Boude's post at http://www.dougboude.com/blog/1/2007/10/Appropriate-Usage-of-the-THIS-Scope.cfm, prompted me to create this simple test that should show the difference between using the "this" scope, the "variables" scope and the "var" scope. Here is what you need to get things setup. Declare a test component as follows:
<cfcomponent displayname="thisScopeToUseOrNotToUse">
 <cffunction name="testMethod">
  <!--- Create a variable in the local scope of the function --->
  <cfset var iAmAccessableOnlyWithinThisMethod = true />

  <!--- Create a variable in the "this" scope --->
  <cfset this.iAmAccessableFromOutside = true />

  <!--- Create variable in the private component scope --->
  <cfset variables.iAmAccessableOnlyWithinThisCFC = true />
 </cffunction>

 <cffunction name="getIAmAccessableOnlyWithinThisCFCButCanBeViewedFromTheOutsideWithAPublicMethod">
  <cfreturn variables.iAmAccessableOnlyWithinThisCFC />
 </cffunction>
</cfcomponent>
Then declare a test file as follows:
<cfset testCFC = createObject("component", "thisScopeToUseOrNotToUse") />
<cfset testCFC.testMethod() />
=======================================
<p />
Try to read a variable declare in the "this" scope of the component
<p />
<strong>iAmAccessableFromOutside:</strong>
<cfdump var="#testCFC.iAmAccessableFromOutside#" />
<p />
=======================================
<p />
Try to read a variable declared in the private component scope (the "variables" scope)
<p />
<strong>iAmAccessableOnlyWithinThisCFC:</strong>
<cftry>
 <cfdump var="#testCFC.iAmAccessableOnlyWithinThisCFC#" />
<cfcatch type="any">
 <cfdump var="#cfcatch.message#" />
</cfcatch>
</cftry>
<p />
=======================================
<p />
Using a public method to read a variable in the private component scope (the "variables" scope):
<p />
<strong>getIAmAccessableOnlyWithinThisCFCButCanBeViewedFromTheOutsideWithAPublicMethod:</strong>
<cfoutput>#testCFC.getIAmAccessableOnlyWithinThisCFCButCanBeViewedFromTheOutsideWithAPublicMethod()#</cfoutput>
<p />
=======================================
<p />
<strong>Trying to read a variable declared only in the method scope:
<p />
iAmAccessableOnlyWithinThisMethod:</strong>
<cftry>
 <cfdump var="#testCFC.iAmAccessableOnlyWithinThisMethod#" />
<cfcatch type="any">
 <cfdump var="#cfcatch.message#" />
</cfcatch>
</cftry>
Here are the results: ColdFusion "this" Scope Test Now, for the explanation:
  1. First we create a instance of the compontent with "createObject"
  2. Call the "testMethod" of the component so we can set the variables
  3. Then we try to access the "iAmAccessableFromOutside" variable and get "true" since that was the value set to the variable in the "testMethod" function
  4. Next, we try to read a variable declared in the private component scope "iAmAccessableOnlyWithinThisCFC". The code block is wrapped with "cfcatch" so we get a nice error message when ColdFusion cannot access the privately declare variable
  5. However, we can use the method "getIAmAccessableOnlyWithinThisCFCButCanBeViewedFromTheOutsideWithAPublicMethod" which just returns the private scope variable "iAmAccessableOnlyWithinThisCFC"
  6. Lastly, we try to access the variable "iAmAccessableOnlyWithinThisMethod" which was declared in the private "var" scope of the "testMethod" function. The code block is wrapped with "cfcatch" so we get a nice error message when ColdFusion cannot access the privately declare variable
// //]]>