1

I have been reading about the CF Scopes, and am comfortable with the CFC scopes and their implications (detailed here), however, whenever I search for CF scopes it almost always references in the context of a CFC - So I was hoping for some clarification around scopes in CFM pages. I am working with CF 9/10 so only really interested in how the scopes behave in these releases.

  1. What scopes are available on a CFM page - do CFM pages have the same concurrency problems as can happen elsewhere, or are the variables scoped on a CFM page bound to the scope of that specific request?

  2. If I include the line <cfset myVar = 10 /> in a CFM page , which scope will it be included in? is there any risk of either other users on the same page accessing the variable or other cfm pages accessing it?

Thanks

rhinds
  • 9,976
  • 13
  • 68
  • 111

2 Answers2

2

Almost all the scopes except 'THIS' are available in the CFM pages.

unscoped variables declared in CFM page can be called directly or can be called with VARIABLES scope prefix.

eg:

<cfset varA = 'someValue'/>

can also be written as

<cfset VARIABLES.varA = 'something' />

To my Knowledge, unless you create a singleton (only possible for CFC's) and put it in Application scope, you never risk sharing variables with other users. This is also valid if one is not careful about scoping the local variables properly in the CFC functions.

On a CFM page, each user request has its own processing thread and never gets crossed with other user request. So, the variables are bound only to the scope of that specific request.

if you want a variables to be used by all the users requesting a page, you can put that in the APPLICATION scope.

I did not quite understand your second question. if you can elaborate on it, may be i can add more to my answer.

Update

This code will help you answer the 2 questions.

<cfscript>
    function a(){
        _a = 20;
        WriteOutput("Inside function:"&variables['_a']);
        WriteOutput("Inside function:"&variables['_b']);
    }
    _b = 30;
    a();
    WriteOutput('outside function:'&variables['_a']);
</cfscript>

Output

Inside function:20
Inside function:30
outside function:20

Sanjeev
  • 1,838
  • 1
  • 16
  • 28
  • Cool thanks - 2 further Q's then: 1) if I have something defined in variables scope on a cfm, that variable scope is not available anywhere else at all unless I pass that scope as an argument? 2) if I have a cfm with a function on it, does the function have access to the cfm page variables scope? Do unscoped function variables default to the cfm page variable scope? – rhinds Apr 04 '13 at 21:27
  • also, have just read this http://blog.alexkyprianou.com/2010/09/20/variables-scope-in-coldfusion/ seems to suggest that two requests to the same cfm page would have access to the same variables scope? If that's true, I assume the variables scope would not be thread safe even in plain cfm code? – rhinds Apr 05 '13 at 20:00
  • @rhinds I have not come across the race condition. I shall check that and find if its a bug or intended feature. – Sanjeev Apr 07 '13 at 05:08
  • @rhinds I could not replicate the content as shown in blog. BTW, this is already in discussion on your other question. So, I shall continue it there. – Sanjeev Apr 07 '13 at 05:21
1

This page, gives a good explanation of the available scopes.

If you look hard enough, you will find more information about what happens if you don't scope your variables. The gist of it is that your code will run successfully, but less efficiently. The reason is that ColdFusion will attempt to find the correct scope. It checks certain scopes, in a specified order. That order is somwhere, I just couldn't find it quickly.

For your second question,

<cfset myVar = 10>

puts the myVar variable into the variables scope.

Regarding one user changing variables that affect other users, I believe the only scope that is at risk is the application scope. However, with modern browsers, it is possible for a single user to mess up his own session variables. I've seen it done.

Another way that variables might be inadvertently changed is with functions. If you want to keep your variables local to the function, you have to use the var keyword when you instantiate them. In later versions of CF, there is a local scope that accomplishes the same thing.

Personally, I scope all my variables except for the variables scope.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • 1
    What you said about variables bleeding over into the form scope is incorrect. Running this code ` ` we correctly get `form.myvar='b'`. If you try and do something like `` then, yes it'll search the `form` and `url` scopes, but I've never seen it the other way around. – Busches Mar 28 '13 at 12:53
  • Good point. I guess I should have tested it before I wrote it. – Dan Bracuk Mar 28 '13 at 13:19
  • 1
    see also http://stackoverflow.com/questions/1152248/in-coldfusion-variables-are-resolved-in-what-order – genericHCU Mar 28 '13 at 16:02
  • @DanBracuk if you see my comment on the other answer, http://blog.alexkyprianou.com/2010/09/20/variables-scope-in-coldfusion/ this seems to suggest that the variables scope isn't thread safe in cfm pages? – rhinds Apr 05 '13 at 20:06