Creating Components

Creating a CFC is easy. You simply define the component using the cfcomponent tag and save the file with a .cfc extension. cfcomponent is a paired tag that acts as a container for the CFC. Within the component, you can have any number of properties (defined by the cfproperty tag) and methods (each defined using a pair of cffunction tags). Each method can accept zero or more arguments, defined with cfargument tags. If a CFC returns any data, it does so with the cfreturn tag. Essentially, a component is a collection of related UDFs grouped within a cfcomponent container. Syntactically, a basic CFC looks like this:

<cfcomponent>
  <cfproperty />
  
  constructor code...
   
  <cffunction>
    <cfargument>
    CFML
    <cfreturn>
  </cffunction>
   
  <cffunction>
    <cfargument>
    CFML
    <cfreturn>
  </cffunction>
</cfcomponet>

The cfcomponent tag has four optional attributes:

displayname=" display_name "

Used to display a descriptive name for the component in ColdFusion MX’s Component Browser or when the component is introspected using the getMetaData( ) function. These concepts are discussed later in the chapter.

hint=" text "

Used to display a hint describing what the component does in ColdFusion MX’s Component Browser or when the component is introspected using the getMetaData( ) function.

extends=" component_name "

Name of a parent component from which the component should inherit methods and properties.

output="yes|no"

Indicates whether the component’s constructor code (any area within the

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.