Coldfusion Scopes Clarification

2019-08-01 23:34发布

问题:

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

回答1:

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



回答2:

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.