2

I tried to rewrite my Application.cfc and other .cfc files in my system with cfscript. There is few things that I'm not sure how they work in cfscript. I'm wondering about defining variables inside of the functions. For example onRequestStart() function looks like this:

function onRequestStart(required string thePage) returntype="boolean" output="false" {
        var request.appCode = "MyApp";
        var request.appName = "Single Page Application";
        var page = listLast(arguments.thePage,"/");
        var onApplicationStart();

        if(!listFindNoCase("Home.cfm,Auth.cfc",page)){
            if(structKeyExists(SESSION, "loggedin") AND SESSION.loggedin EQ false){
                location(url="https://www.myapp.org", addToken="false");
            }
        }

        return true;
    }

Do I need to use var word in situations where I'm defining request/session variables? If I do what is the best practice, use var word or use local.variablename? Is local and variables same in cfscript?

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • Nothing to do with the question. Why are you calling onApplicationStart at the beginning of every request? – SOS Jun 26 '18 at 17:37
  • @Ageax I use that in case when I need to restart application variables. That line is commented out. I'm still in development process. Hope this make sense. – espresso_coffee Jun 26 '18 at 17:52
  • 1
    Gotcha. Just wasn't sure if it was going into Prod, which would be .. bad ;-) – SOS Jun 26 '18 at 19:46
  • Btw., if you want to, you can restart your application when a specific URL parameter is set. See https://stackoverflow.com/a/14362012/432681. – Sebastian Zartner Jun 26 '18 at 21:00
  • Also see: https://stackoverflow.com/questions/47112598/scope-of-var-and-variables/47114142#47114142 – James A Mohler Jul 04 '18 at 04:23

2 Answers2

7

var is used only for local variables. That means variables which are/should not be accessible outside the function definition. Session & Request are accessible to each session & request respectively. Putting them on var scope will give terrible results.

You can use either var or local, both have 'local' scope. Variables is the page scope and any variable defined in the Variables scope will be accessible to all functions in the CFC.

function onRequestStart(required string thePage) returntype="boolean" output="false" {
    request.appCode = "MyApp";
    request.appName = "Single Page Application";
    var page = listLast(arguments.thePage,"/");
  //this is a function call and not variable declaration.   
  onApplicationStart();

    if(!listFindNoCase("Home.cfm,Auth.cfc",page)){
        if(structKeyExists(SESSION, "loggedin") AND SESSION.loggedin EQ false){
            location(url="https://www.myapp.org", addToken="false");
        }
    }

    return true;
}   
SOS
  • 6,430
  • 2
  • 11
  • 29
CFML_Developer
  • 1,565
  • 7
  • 18
  • I wasn't sure how this can be replaced in CFML `` since I do not need to define anything in this case just call the function. thanks for explaining variables and scopes. – espresso_coffee Jun 26 '18 at 14:39
  • 1
    Just drop the cfset and add a semi-colon at the end: `structDelete(session, "username");` – SOS Jun 26 '18 at 17:38
3

var is not equal to the <cfset> tag, i.e. you can't do a simple search & replace when switching to CFScript syntax.

var is only used for local variable definitions. This means, setting structure and array items, like request, session and other scope variables should not be prefixed by var. Also, function calls must be written without preceding var.

local and var both refer to the local scope. Though note, as mentioned above, if you want to define variables via local.something the var keyword is also not needed.

variables, in contrary to local, refers to the page scope, which is accessible from everywhere within the component and any included pages.

For more info on the different scopes, you should read the Adobe docs.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
  • Good points. Definitely shouldn't use var on persistent scopes. Though curiously var behaves differently with persistent scopes than local - and what I'd have expected. https://trycf.com/gist/1e19e31113a38d682a5ca130f10d83db/acf2016?theme=monokai – SOS Jun 27 '18 at 15:06
  • 1
    Interesting! Note that Adobe ColdFusion and Lucee behave differently in this case. Obviously, Adobe ColdFusion just ignores the `var` and assigns the variable to the global scope. Lucee is more consistent and handles `var` and `local.` equally putting the variables into the local scope. Another interesting thing to note is that the structure variable holding `firstVar` and `secondVar` gets automatically defined. – Sebastian Zartner Jun 27 '18 at 20:20
  • I hadn't notice Lucee's behavior was different. It's handling makes more sense to me. Guess it's not surprising. There's a few areas, like with scopes, where Adobe CF's handling is a bit of IMO. – SOS Jun 27 '18 at 22:57