Application variables are a special variable type that can be used within ColdFusion applications. Application variables could be compared with global variables in other types of application programming because after an application variable is created, it is accessible to all users of the application. Application variables are intended to define variables and settings that remain constant throughout your entire application. It is common for datasource names to be set to the application scope. Application variables must be referred to using the application prefix. An application variable looks like this: <cflock scope="application" type="exclusive" timeout="10"> <cfset application.DSN="ICFMX"> </cflock> CFAPPLICATION TagTo make use of application scoped variables, you must place a call to the CFAPPLICATION tag. By calling this tag, you initialize and name the application and have the capability to define other aspects of the application as well. The CFAPPLICATION tag is typically called within the Application.cfm.It initializes the application and sets the application name. It also defines the scoping of variables within the application, enables the storage of client variables, and sets a location for the storage of those variables. The CFAPPLICATION tag also enables the use of session variables in your ColdFusion application. Until you've enabled the use of application and session variable scopes within the CFAPPLICATION tag, you cannot use them within your application. Not only do you enable the use of application- and session-scoped variables within the CFAPPLICATION tag, but also you use it to set their timeout period. <cfapplication name="InsideCFMX" sessionmanagement="Yes" sessiontimeout="#CreateTimeSpan("0,0,60,0")#"> The CreateTimeSpan() function is used to specify the timeout for the session variables within this application. Using Application VariablesNow that you've enabled the use of application variables in your application, you can begin to use them. Because application variables are global to your application after they are set, your code should never assume that they have or have not been set. You should always test for the existence of an application variable prior to setting it. The reason is that you don't want to unnecessarily set the value of any variable that is stored in memory. <cflock scope="application" type="exclusive" timeout="10"> <cfparam name="application.dsn" default="insidecfmx"> </cflock> You might have also seen the same functionality in a CFIF statement. CFPARAM is cleaner and more performance-efficient. <cflock scope="application" type="exclusive" timeout="10"> <cfif NOT IsDefined("application.dsn")> <cfset application.dsn="insidecfmx"> </cfif> </cflock> Application variables, such as a datasource name, might be used over and over throughout your application. They might also be used on the same template in several places. Because application variables are shared memory variables, it makes sense to convert these variables to another variable scope prior to using them in our templates. There are two reasons for this:
The application scope is a structure. Deleting application variables is as easy as deleting any value held in a structure. You use the StructDelete() function and specify the structure name and key to be deleted: <cflock scope="application" type="exclusive" timeout="10"> <cfif NOT IsDefined("application.dsn")> <cfset tmp=StructDelete(application, "dsn")> </cfif> </cflock> As we've said, application variables are great for setting variables that stay the same throughout the application. If you want to set variables that are specific to a single user, or to a single session of that specific user, you might consider the use of session variables. |