The Thinking Lemur

22 Apr, 2007

Scoping in CFC

Posted by: Donnie Bachan In: ColdFusion

This topic has been beaten to death by all the gurus in the CF community but I just noticed a funny little thing that I thought I would share. Ray Camden did his wonderful cheatsheet for CFC scopes and noted the differences, you can view it here.

Now after reading this you would assume that the arguments scope and the var scope to be different, at least that is what I inferred. After all we could repeat the names of variables in different scopes, so we could do:

<cfscript>
variables.foo = 12;
request.foo = 15;
session.foo = “fifteen”;
etc…
</cfscript>

All these variables would be valid but apparently you cannot do that with arguments and var variables in a function. The following code generates an error saying that the variables fdata was declared twice.

<code>
<cffunction ..>
<cfargument name=”fdata” required=”true” />
<cfset var fdata = arguments.fdata />
</cffunction>
</code>

Is this a bug? Or how it should be? To me that is a bug. I wanted to refer to the fdata variable separately from arguments.fdata, not that it is the best programming practice but you should be allowed to create variables in this way as long as you prefix the variables with the scope of course.

No Responses to "Scoping in CFC"

Comment Form

You must be logged in to post a comment.