0

I am starting to use the LOCAL scope in ColdFusion 9. I am trying to figure when to use it and when NOT to use it. Can you tell me the advantages/disadvantages of using the LOCAL scope either way? Should LOCAL be used on every page, or just in functions?

WHICH SHOULD BE USED?

<cfset ImageHeight = 36>  
<cfset ImageWidth = 42>  
<cfoutput>
<img src="" height="#ImageHeight#" width="#ImageWidth#">
</cfoutput>

OR

<cfset LOCAL.ImageHeight = 36>  
<cfset LOCAL.ImageWidth = 42>  
<cfoutput>
<img src="" height="#LOCAL.ImageHeight#" width="#LOCAL.ImageWidth#">
</cfoutput>
3
  • Wait, does that mean you have to add six characters, five of which are ugly upper-case, to get any scoping (except global one) in ColdFusion? Wow. And people are bitching about PHP...
    – user7043
    May 3 '11 at 19:52
  • 1
    @delnan - the casing is a personal preference in coding style and is nothing to do with ColdFusion. The local scope, as Justice has already pointed out, is only usable in functions in ColdFusion 9. Access to other scopes in ColdFusion is not that different to PHP. May 3 '11 at 20:57
  • I always use upper case for scoping. It's not required. Since I have nothing else in all caps, it makes the scope easier to read.
    – Evik James
    May 4 '11 at 18:20
3

The local scope is defined only within functions.

function a() {
  //this way
  var myVariable = 0;
  //is the same as this way (in CF9)
  local.myVariable = 0;
}

Do not use the local scope outside functions.

Outside functions, variables default to the variables scope.

//this way
myVariable = 0;
//is the same as this way
variables.myVariable = 0;
1
  • That is what I thought, but I couldn't find any clear documentation. Thanks!
    – Evik James
    May 4 '11 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.