78
Chapter 3: Using ColdFusion Variables
Caution:
Do not call
StructClear
(Session)
to clear session variables. This deletes the
SessionID
,
CFID
, and
CFtoken
built-in variables, effectively ending the session. If you want to use
StructClear
to
delete your application variables, put those variables in a structure in the Session scope, then clear
that structure. For example, put all your application variables in Session.MyVars and then call
StructClear(Session.MyVars)
to clear the variables.
Using the This scope outside CFCs (advanced topic)
The This scope is specifically designed for use with ColdFusion Components (CFCs). For
information on CFCs and the This scope, see
Chapter 11, “Building and Using ColdFusion
Components,” on page 219
. However, you can also use the This scope without having a CFC if
you do the following:
•
Create a UDF using the
cffunction
tag.
•
Assign the UDF to a containing object such as a structure or persistent scope.
In this case, the This scope inside the function is a reference to the containing object, and you
can therefore access the same variables.
•
Specify This as the prefix, inside the function.
•
Specify the containing object name as the prefix, outside the function.
The following code shows how this works:
<cffunction name="TestFunction" >
<cfparam name="This.foo" default="Original This.foo">
dumping This inside the function:<br>
<cfdump var="#This#">
</cffunction>
First, just call the function<br>
<cfset TestFunction()>
Right now, the This variable is NOT a scope reference, just a structure.
<br><br>
Now put function in a structure and dump the structure<br>
Note that there is no foo variable in the structure.
<cfset newStruct.TestFunction=TestFunction>
<cfdump var="#newStruct#">
<br>
Now change newStruct.foo and call the function <br>
<cfset newStruct.foo="New This.foo">
<cfset newStruct.TestFunction()>
Note that now we've changed the foo variable from outside the function,<br>
And the function itself is part of the This scope!
Ensuring variable existence
ColdFusion generates an error if you try to use a variable value that does not exist. Therefore,
before you use any variable whose value is assigned dynamically, you must ensure that a variable
value exists. For example, if your application has a form, it must use some combination of
requiring users to submit data in fields, providing default values for fields, and checking for the
existence of field variable values before they are used.
There are several ways to ensure that a variable exists before you use it, including:
•
You can use the
IsDefined
function to test for the variable’s existence.
•
You can use the
cfparam
tag to test for a variable and set it to a default value if it does not exist.
Содержание COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...