

|
|
Variables in CFML; Dynamic VariablesColdFusion offers significant, perhaps unique flexibility in the many ways that a developer can programmatically build dynamic references to variables. Much of this flexibility results from the way ColdFusion automatically organizes all variables into scopes and those scopes into structures. ColdFusion also provides a direct syntax for assigning data to dynamic variables and several related functions. Determining Variable ExistenceThe ColdFusion function IsDefined() is used to determine the existence of a variable (it replaced the now deprecated function ParameterExists() which did not allow for dynamic variable processing). The function takes a string (or a variable containing a string or functions resulting in a string) and determines if there is a matching, existing variable. The function returns TRUE (the variable exists) or FALSE (the variable does not exist). Some examples of IsDefined() (the last two examples are functionally equivalent):
Variables in ColdFusion either exist (and have values) or don't exist at all. It's worth noting that because Arrays in CFML are dynamically sized and allocated it's quite difficult to reliably determine the existence of a specific element in an array. Dynamic Variable FunctionsCFML provides two functions for working with dynamic variables: Evaluate() and SetVariable(). The Evaluate() function is used to obtain and return the value of a dynamic variable. Much like the IsDefined() function it takes a string and determines if a variable of that name exists. If the variable does exist the value of it is returned, if it doesn't an error is thrown. Evaluate() can be used to convert variables or other functions within larger strings to simple string values. Such dynamic strings can be stored in a database or other location for later use. The following is an example of this: <cfset CustomerName="Jane"> The SetVariable() function is used to set the value of a dynamic variable. It takes a string (or a variable containing a string) and a value. It then assigns the specified value to the variable specified creating it if needed. In early versions of ColdFusion the only way to manipulate dynamic variables was through the use of these functions. The techniques available in later versions make these functions less appropriate in many cases but you will often still see them in use. Old habits die hard. Using Scopes as StructuresScopes accessible as structures (in CFMX, all of them) present many opportunities for dynamic variable manipulation. As noted already Indexed Notation may be used to construct dynamic references to these scopes. Some examples of this:
Lastly any tag or function that will accept a structure will accept a named scope. You can view the contents of any named scope using <CFDUMP> (available in CF 5.0 and above) or perform actions on all variables in a scope using <CFLOOP> and the COLLECTION attribute. Dynamic Variables to the Left of the AssignmentColdFusion versions 5.0 and above allow developers to use dynamic variable syntax to the left as well as the right of the assignment operator ("="). All of the following are valid CFML statements:
The flexibility afforded by this is substantial. Almost all scenarios requiring the SetVariable() function in previous versions can be dealt with using this capability. |