ColdFusion 9 CFC Var Scoping

Posted: January 28, 2011 in ColdFusion

Recently I was assigned a new client that uses mostly ColdFusion 9 for their applications. In the past the majority of my clients have not taken the opportunity to upgrade from version 8, and there has not really been much reason to that I’ve run into as well. However, this opportunity creates the needed effort to make sure I know the differences between 8 and 9, such as var scoping.

As you probably know the variables scope in a CFC is private to that CFC. In other words the object calling that component may not access this scope’s values until it is returned to it. This scope can be set using the “variables” prefix to the variable name. If you skip the scope, it will be assigned to the variables scope. The following code sets the same variable and will persist between calls to each method the component contains.

<cffunction>
    <cfset variables.var1 = "Example 1">
    <cfset var1 = "Example 2">
    <!--- resulting in both variables.var1 and var1 values set to "Example 2" --->
</cffunction>

Normally you would want to declare variables private to each method so that your values are never overwritten by other process. This is normally done by using the var keyword. However in ColdFusion 9 you may use the local scope instead. This local scope does not persist between calls to the component’s methods. For example the following two lines work the same (the var keyword would not be required in this instance if using CF9):

<cffunction>
    <cfset var var1="Example 1">
    <cfset local.var1="Example 2">
    <cfreturn local.var1> <!--- Returns "Example 2", returning var1 would result in an error as it does not exist --->
</cffunction>

The question now becomes “What if I var scoped a local struct to use in previous versions of ColdFusion (var local={})?” The answer is you are good to go. ColdFusion 9 will just create a struct named “local” in the local scope (local.local).  So if you are coding for CF9 there is no reason to var scope a local struct as you would just be creating an unused struct. Instead use the already existent local scope. But you don’t need to go back and re-factor your code to remove all the local var scopes as an empty struct will not cause any harm unless you where counting on local.local to be there in the past. The following example is backwards compatible in CF9.

<cffunction>
    <cfset var local = {}> <!--- In CF9 this will result in local.local, so leave this out --->
    <cfset local.var1 = "Example1">
    <cfreturn local.var1>
</cffunction>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s