User Guide

Table Of Contents
Creating ColdFusion components 205
The component page defines methods (functions), properties (data), or both. Most CFCs have
methods, or methods and properties, but you can also have a CFC that contains only
properties.
You use the cffunction tag to define CFC methods. The CFScript function statement can
create simple methods, but it does not provide options to control access to the method, provide
metadata, specify a return type, or control generated output.
You can write code on the component page that is outside of cffunction definitions. This
code executes when the CFC is instantiated or whenever you invoke a method of the CFC.
Building ColdFusion components
You use the
cfcomponent and cffunction tags to create ColdFusion components. By itself, the
cffunction tag does not provide functionality. The cfcomponent tag provides an envelope that
describes the functionality that you build in CFML and enclose in
cffunction tags. The
following example shows the skeleton of a component with two methods:
<cfcomponent>
<cffunction name="firstMethod">
<!--- CFML code for this method goes here. --->
</cffunction>
<cffunction name="secondMethod">
<!--- CFML code for this method goes here. --->
</cffunction>
</cfcomponent>
Defining component methods
You define component methods using
cffunction tags. The following example defines a CFC
that contains two methods,
getall and getsalary:
<cfcomponent>
<cffunction name="getall" output="false" returntype="query">
<cfset var queryall="">
<cfquery name="queryall" datasource="cfdocexamples">
SELECT * FROM EMPLOYEE
</cfquery>
<cfreturn queryall>
</cffunction>
<cffunction name="getsalary" output="false">
<cfset var getNamesandSalary="">
<cfquery name="getNamesandSalary" datasource="cfdocexamples">
SELECT FirstName, LastName, Salary FROM EMPLOYEE
</cfquery>
<cfreturn getNamesandSalary>
</cffunction>
</cfcomponent>