Adobe COLDFUSION 9 Manual page 179

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

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
This way you only include the functions once per request and they are available throughout the life of the request. For
example, create a myFuncs.cfm page that defines your functions and assigns them to the Request scope using syntax
such as the following:
function MyFunc1(Argument1, Argument2)
{ Function definition goes here }
Request.MyFunc1 = MyFunc1
The application page includes the myFuncs.cfm page:
<cfinclude template="myfuncs.cfm">
The application page and all custom tags (and nested custom tags) call the functions as follows:
Request.MyFunc1(Value1, Value2)
Using the Request scope for static variables and constants
You can partially break the rule described in the section
defines variables in the Request scope. However, it is a specific solution to a specific issue, where the following
circumstances exist:
• Your function initializes a large number of variables.
• The variables have either of the following characteristics:
• They must be static: only the function uses them, the function can change their values, and their values must
persist from one invocation of the function to the next.
• They are named constants; that is the variable value never changes.
• Your application page (and any custom tags) calls the function multiple times.
• You can assure that only the function uses the variable names.
In these circumstances, you can improve efficiency and save processing time by defining your function's variables in
the Request scope, rather than the Function scope. The function tests for the Request scope variables and initializes
them if they do not exist. In subsequent calls, the variables exist and the function does not reset them.
The
function, written by Ben Forta and available from www.cflib.org, takes advantage of this
NumberAsString
technique.
Using function names as function arguments
Because function names are ColdFusion variables, you can pass a function's name as an argument to another function.
This technique allows a function to use another function as a component. For example, a calling page can call a
calculation function, and pass it the name of a function that does some subroutine of the overall function.
This way, the calling page could use a single function for different specific calculations, such as calculating different
forms of interest. The initial function provides the framework, while the function whose name is passed to it can
implement a specific algorithm that the calling page requires.
The following simple example shows this use. The
function that performs a specific binary operation and two operands. The
function and passes it the operands. This code defines a single operation function, the sum function. A more complete
implementation would define multiple binary operations.
"Referencing caller
function is a generalized function that takes the name of a
binop
Last updated 8/5/2010
variables" on page 165. Here, the function
function simply calls the specified
binop
174

Advertisement

Table of Contents
loading

Table of Contents