User Guide

Table Of Contents
206 Chapter 10: Building and Using ColdFusion Components
Because component methods are ColdFusion functions, most of their features and coding
techniques are identical to those of user-defined functions. For more information on using the
cffunction tag to create functions, see Chapter 9, “Writing and Calling User-Defined
Functions,” on page 171. Like other ColdFusion functions, CFC methods can display
information directly by generating output, or can return a value to the code or client that invoked
the method.
You use the following
cffunction tag attributes only for CFCs:
The displayname and hint attributes, which document the CFC; for more information, see
“Documenting CFCs” on page 214.
The access attribute, which controls access to the CFC; for more information, see “Using
access security” on page 235.
For detailed reference information on the
cffunction tag, see CFML Reference.
Defining CFCs with related methods
When defining CFCs, it is good programming practice to organize related methods in one CFC.
For example, you could put all methods that perform operations related to a user, such as
addUser, editUser, and storeUserPreferences, in one CFC. You can group related
mathematical functions into one CFC. A CFC can also contain all the methods and properties
necessary for a shopping cart. The following CFC contains two
cffunction tags that define two
component methods,
getEmp and getDept. When invoked, the component methods query the
ExampleApps database. The
cfreturn tag returns the query results to the client, or page, where
the method was invoked.
<cfcomponent>
<cffunction name="getEmp">
<cfset var empQuery="">
<cfquery name="empQuery" datasource="cfdocexamples" dbtype="ODBC" >
SELECT FIRSTNAME, LASTNAME, EMAIL
FROM tblEmployees
</cfquery>
<cfreturn empQuery>
</cffunction>
<cffunction name="getDept">
<cfset var deptQuery="">
<cfquery name="deptQuery" datasource="cfdocexamples" dbtype="ODBC" >
SELECT *
FROM tblDepartments
</cfquery>
<cfreturn deptQuery>
</cffunction>
</cfcomponent>