Adobe COLDFUSION 9 Manual page 178

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Using Application.cfm and function include files
Consider the following techniques for making your functions available to your ColdFusion pages:
• If you consistently call a small number of UDFs, consider putting their definitions on the Application.cfm page.
• If you call UDFs in only a few of your application pages, do not include their definitions in Application.cfm.
• If you use many UDFs, place their definitions on one or more ColdFusion pages that contain only UDFs. You can
include the UDF definition page in any page that calls the UDFs.
The next section describes other techniques for making UDFs available to your ColdFusion pages.
Specifying the scope of a function
User-defined function names are essentially ColdFusion variables. ColdFusion variables are names for data. Function
names are names (references) for segments of CFML code. Therefore, like variables, functions belong to scopes.
About functions and scopes
Like ColdFusion variables, UDFs exist in a scope:
• When you define a UDF, ColdFusion puts it in the Variables scope.
• You can assign a UDF to a scope the same way you assign a variable to a scope, by assigning the function to a name
in the new scope. For example, the following line assigns the MyFunc UDF to the Request scope:
<cfset Request.MyFunc = Variables.MyFunc>
You can now use the function from any page in the Request scope by calling Request.MyFunc.
Selecting a function scope
The following table describes the advantages and disadvantages of each function scope:
Scope
Considerations
Application
Makes the function available across all invocations of the application. Access to UDFs in Application scope is
multithreaded and you can execute multiple copies of the UDF at one time.
Request
Makes the function available for the life of the current HTTP request, including in all custom tags and nested
custom tags. This scope is useful if a function is used in a page and in the custom tags it calls, or in nested custom
tags.
Server
Makes the function available to all pages on a single server. In most cases, this scope is not a good choice because
in clustered systems, it only makes the function available on a single server, and all code that uses the function
must be inside a cflock block.
Session
Makes the function available to all pages during the current user session. This scope has no significant advantages
over the Application scope.
Using the Request scope
You can effectively manage functions that are used in application pages and custom tags by doing the following:
1
Define the functions on a function definitions page.
On the functions page, assign the functions to the request scope.
2
Use a
tag to include the function definition page on the application page, but do not include it on any
3
cfinclude
custom tag pages.
Always call the functions using the request scope.
4
Last updated 8/5/2010
173

Advertisement

Table of Contents
loading

Table of Contents