ColdFusion Variable Types
ColdFusion variables come in many different types. Firstly, all variables belong to a scope. There are many different scopes and each variable created in your application belongs to one of these scopes. Secondly, all variables can be either local or global. Thirdly, all variables are either persistent or non-persistent.
Variable Scopes
A variable's scope is determined by its origin. The scope determines a number of properties about the variable, such as its life span, timeout, and storage location, and therefore, how it can be used.
Below is a list of the available scopes for ColdFusion variables:
Scope |
Description |
---|---|
Application |
Contains variables that are associated with one, named application on a server. The cfapplication tag name attribute or the Application.cfc This.name variable setting specifies the application name. |
Arguments |
Variables passed in a call to a user-defined function or ColdFusion component method. |
Attributes |
Used only in custom tag pages and threads. Contains the values passed by the calling page or cfthread tag in the tag's attributes. |
Caller |
Used only in custom tag pages. The custom tag's Caller scope is a reference to the calling page's Variables scope. Any variables that you create or change in the custom tag page using the Caller scope are visible in the calling page's Variables scope. |
CGI |
Contains environment variables identifying the context in which a page was requested. The variables available depend on the browser and server software. |
Client |
Contains variables that are associated with one client. Client variables let you maintain state as a user moves from page to page in an application, and are available across browser sessions. By default, Client variables are stored in the system registry, but you can store them in a cookie or a database. Client variables cannot be complex data types and can include periods in their names. |
Cookie |
Contains variables maintained in a user's browser as cookies. Cookies are typically stored in a file on the browser, so they are available across browser sessions and applications. You can create memory-only Cookie variables, which are not available after the user closes the browser. Cookie scope variable names can include periods. |
Flash |
Variables sent by a SWF movie to ColdFusion and returned by ColdFusion to the movie. |
Form |
Contains variables passed from a Form page to its action page as the result of submitting the form. (If you use the HTML form tag, you must use method="post".) |
Local (function local) |
Contains variables that are declared inside a user-defined function or ColdFusion component method and exist only while a function executes. |
Request |
Used to hold data that must be available for the duration of one HTTP request. The Request scope is available to all pages, including custom tags and nested custom tags, that are processed in response to the request. This scope is useful for nested (child/parent) tags. This scope can often be used in place of the Application scope, to avoid the need for locking variables. Several chapters discuss using the Request scope. |
Server |
Contains variables that are associated with the current ColdFusion server. This scope lets you define variables that are available to all your ColdFusion pages, across multiple applications. |
Session |
Contains variables that are associated with one client and persist only as long as the client maintains a session. They are stored in the server's memory and can be set to time out after a period of inactivity. |
This |
Exists only in ColdFusion components or cffunction tags that are part of a containing object such as a ColdFusion Struct. Exists for the duration of the component instance or containing object. Data in the This scope is accessible from outside the component or container by using the instance or object name as a prefix. |
ThisTag |
Used only in custom tag pages. The ThisTag scope is active for the current invocation of the tag. If a custom tag contains a nested tag, any ThisTag scope values you set before calling the nested tag are preserved when the nested tag returns to the calling tag. The ThisTag scope includes three built-in variables that identify the tag's execution mode, contain the tag's generated contents, and indicate whether the tag has an end tag.A nested custom tag can use the cfassociate tag to return values to the calling tag's ThisTag scope. |
Thread |
Variables that are created and changed inside a ColdFusion thread, but can be read by all code on the page that creates the thread. Each thread has a Thread scope that is a subscope of a cfthread scope. |
thread local |
Variables that are available only within a ColdFusion thread. |
URL |
Contains parameters passed to the current page in the URL that is used to call it. The parameters are appended to the URL in the format ?variablename = value&variablename=value...; for example www.MyCompany.com/inputpage.cfm?productCode=A12CD1510&quantity=3.
|
"Scoping" your Variables
When you set or read a variable, it is good practice to tell ColdFusion which scope it belongs to. Although this is not required, it will avoid any confusion around whether the correct variable is being used or not. It can also improve performance, because ColdFusion must search for variables when you do not specify the scope.
You may occasionally encounter two or more variables with the same name, but belonging to a different scope. To avoid the wrong one being used, you should scope your variables.
You scope a variable simply by prefixing the variable name with the name of the scope (and separating them with a dot).
For example, to scope a session variable, you would do something like this:
If you do use a variable name without a scope prefix, ColdFusion checks the scopes in the following order to find the variable:
- Local (function-local, UDFs and CFCs only)
- Arguments
- Thread local (inside threads only)
- Query (not a true scope; variables in query loops)
- Thread
- Variables
- CGI
- Cffile
- URL
- Form
- Cookie
- Client
Variable Persistence
ColdFusion provides four variable scopes that let you maintain data that must be available to multiple applications or users or must last beyond the scope of the current request.
These are:
- Client
- Session
- Application
- Server
Adobe has an article that explains more about persistent scope variables in ColdFusion if you're interested.