Skip to main content
June 22, 2011
Question

Scope order: local vs argument

  • June 22, 2011
  • 1 reply
  • 1807 views

Hi,

According to http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09af4-7fdf.html, LOCAL variables take precedence to ARGUMENTS variables.  However, when I access an unscoped variable in a CFC function that is available in both LOCAL and ARGUMENTS, I get back the ARGUMENTS scope.  Why is that?  I am using CF 9,0,0,251028.

<cfcomponent>
   
    <cffunction name="testFunction" access="public" returntype="string">
        <cfargument name="x" default="argument">
       

        <cfset LOCAL.x = "local">
       
        <cfreturn x />

    </cffunction>
   
</cfcomponent>

<cfset obj = createObject( "component", "component.Test" )>

<cfoutput>#obj.testFunction("argument")#</cfoutput>

The output I get back is "argument" instead of "local".

The other thing that I find weird is that if I replace

<cfset LOCAL.x = "local">

with

<cfset var x = "var">

I get the error message "X is already defined in argument scope".

    This topic has been closed for replies.

    1 reply

    Inspiring
    June 22, 2011

    My guess would be "bad implementation on the part of Adobe", at least on the basis of you evidence, anyhow.

    Have you got a repro case? Maybe raise a bug with them.

    --

    Adam

    Legend
    June 23, 2011

    Hmm, I have never heard of the "local" scope. Are you sure the doc is not referring to <cfset var x = "local" /> as being a local variable? Try changing your example to the following:

        <cffunction name="testFunction" access="public" returntype="string">
            <cfargument name="x" default="argument" />
            <cfset var x = "local" />
            <cfreturn x />

        </cffunction>

    June 23, 2011

    Hi Steve,

    Local scope was a new addition to CF9 and is supposed to be the equivalent to doing var varName inside a CFC function.

    Anyway, I found this thread at houseoffusion forum that confirms that local scope is buggy.  http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:61285

    ML