Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 187

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
<cfscript>
serverTime=now();
utcTime=GetTimeZoneInfo();
utcStruct=structNew();
utcStruct.Hour=DatePart("h", serverTime);
utcStruct.Minute=DatePart("n", serverTime);
utcStruct.Hour=utcStruct.Hour + utcTime.utcHourOffSet;
utcStruct.Minute=utcStruct.Minute + utcTime.utcMinuteOffSet;
if (utcStruct.Minute LT 10) utcStruct.Minute = "0" & utcStruct.Minute;
</cfscript>
In the example, the
getUTCTime
getUTCTime.cfm code calculates the UTC time representation of the current time and populates a structure with
hour and minute values. The method in tellTime.cfc then uses the information in the structure to return the current
UTC time as a string to the calling page. The included page must not include a
Initializing instance data
Some components have instance data, which is data that persists as long as the component instance exists. For
example, a shopping cart component can have instance data that includes the IDs and quantities of items that the user
places in the shopping cart. Instance data is often shared by several methods that can create, delete, or modify the data.
You can refer to instance data of a CFC only if you create an instance of the CFC. From inside the CFC, you refer to
instance data of the CFC using the
to instance data using dot notation, including the name of the instance of the component and the name of the instance
data, as in
objectname.ivarname
do not typically have instance data.
You initialize instance data at the top of the component definition, before the method definitions. ColdFusion executes
this code when it instantiates the component; for example, when a cfobject tag creates the component instance.
Because this code executes only when the instance is created and it typically "constructs" properties of the component,
instance data initialization code is sometimes called constructor code.
You can use any CFML tag or function in constructor code, and the code can perform any ColdFusion processing, such
as querying a database or data validation and manipulation. If one component extends another, the parent
component's constructor code executes before the child component's constructor code.
Note: ColdFusion does not require you to place the initialization code at the top of the component definition; however, it
is good programming practice to do so.
The following example shows constructor code for a shopping cart CFC:
<cfcomponent>
<!--- Initialize the array for the cart item IDs and quantities. --->
<cfset This.CartData = ArrayNew(2)>
<!--- The following variable has the ID of the "Special Deal" product for
this session. --->
<cfset This.Special_ID = RandRange(1, 999)>
For information on scopes, see
A useful technique is to define a method named
The
method can initialize constants and return an instance of the component to the calling page. The following
init()
code illustrates an example of an
method definition calls the getUTCTime.cfm file with the cfinclude tag. The
prefix, for example
this
. Components whose methods you invoke without first instantiating the component
"The This
scope" on page 198 and
, which initializes an instance of a CFC, acting as a constructor.
init()
method:
init()
Last updated 1/20/2012
cfreturn
. From the calling page, you refer
this.firstvariable
"The Variables
scope" on page 198.
182
statement.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents