ColdFusion Muse

Local Function Scope in ColdFusion 8

Mark Kruger February 17, 2011 11:01 AM ColdFusion Comments (3)

I was frustrated yesterday by the inability to get at the "local function" scope in ColdFusion 8. In ColdFusion 9 each function has a scope called "local" that functions like the various other scopes in ColdFusion. You can loop through its keys and do things with its properties. In ColdFusion 7 and 8 a great many developers actually created a pseudo local scope by doing something like "CFSET var local = structNew()" and then appending all the needed vars to this structure. In CF 9 the "local" scope was already set up for you so if you did "CFSET var x = 10" you had a var called "local.x" to work with as a member of the local object.

In the ColdFusion 8 code I was working with yesterday I had a rather long and involved function that I was trying to modify. I needed to loop through the "local" scope for one reason or another and the there was not "pseudo" local scope created. I did not want to go back and re-engineer the entire function. I was just reviewing and prototyping and there were around 25 local vars declared. So I asked the question to my list - how do I get at that local function scope without refactoring the whole function?

ColdFusion Guru Jared Rypka-Hauer came up with this undocumented solution.

<cfset var localScope = getPageContext().getActiveFunctionLocalScope()>

Sure enough, this code creates an object with all the local scope vars in it. There are a couple of caveats.

  • The Arguments structure is a part of the local scope (stands to reason right?)
  • This object (as written above) will also belong to the local scope. If you take out the "var" you've added it to the component scope.
  • Since ColdFusion 9 has created a local scope for you, this function and approach will break on a CF 9 server. so if you use it make sure you make a note that you need to modify this bit of code if you move to ColdFusion 9.

Finally, here's a little test function you can use to demonstrate to yourself how this works.

<Cffunction name="checkVarScope">
    
    <cfscript>
        var thisvar = 10;
        var thatvar = 12;
        var theOtherthing = 'Sister Sally';
        localScope = getPageContext().getActiveFunctionLocalScope();
    
</cfscript>
    
    <Cfdump var="#localScope#">

</Cffunction>

<Cfset checkVarScope()>

Here's that function in action - Local Scope.

  • Share:

3 Comments

  • Eric Cobb's Gravatar
    Posted By
    Eric Cobb | 2/17/11 12:18 PM
    VERY COOL! On behalf of all of us still on CF8, let me just say, "Thanks for sharing". This will come in handy.
  • Jake Churchill's Gravatar
    Posted By
    Jake Churchill | 2/17/11 12:45 PM
    Very nice. This is assuming we have properly var scoped code in our CFCs :)
  • Ben Nadel's Gravatar
    Posted By
    Ben Nadel | 2/17/11 8:13 PM
    Very cool. I think there were a few times where something like this would have been a life-saver! Good to have this in the back of my mind going forward.