A ColdFusion component (CFC) is a file saved with the extension .cfc. A CFC can contain data and functions. Within a CFC, data is referred to as properties. Although you use the cffunction tag to define functions within a CFC, they are typically referred to as methods instead of functions.
The page on which you define a CFC is also known as a component page. Component pages use the same tags and functions that regular CFML pages do, plus a small number of special tags (in particular, the cfcomponent tag) and tag attributes. 
You define related methods in a CFC. Unlike ColdFusion custom tags, a single CFC can perform many related actions, defined in multiple methods. The methods can share a data context, such as metadata and scoping, or manage a particular database or set of tables. For example, you can define the methods to insert, update, delete, and retrieve records from a particular database or table in one CFC.

CFCs and object-oriented programming

CFCs are building blocks that let you develop ColdFusion code in an object-oriented manner, although CFCs do not require you to do object-oriented programming. Some of the object-oriented features of CFCs include encapsulation, inheritance, and introspection. CFC object-oriented features are like the object-oriented elements in other languages, like JavaScript.
The technique of incorporating both code and data into one object such as a CFC is known as encapsulation. Encapsulation lets users pass data to and get a result from your CFC without having to understand the underlying code. When you use encapsulation, you can validate data that is passed to the CFC. CFCs can also enforce data types, check for required parameters, and optionally assign default values.
One CFC can inherit the methods and properties of another CFC. Inheritance lets you build multiple specific components without rewriting the code for the basic building blocks of the components. For more information, see Using the Super keyword in Using CFCs effectively.
CFCs support introspection; that is, they can provide information about themselves. If you display a component page directly in an HTML browser, inspect it in the ColdFusion and Adobe Dreamweaver CS3 component browsers, or use the CFML GetMetadata function, you see information about the component. This information includes its path, property, methods, and additional information that you can specify using special documentation attributes and tags. For more information, see Using introspection to get information about components in Using CFCs effectively.
When you use a ColdFusion component, you can invoke a method in the CFC. However, typically, you create an instance of the CFC, and then invoke methods and refer to properties of the CFC.

When to use CFCs

You can use CFCs in the following ways:

  • Developing structured, reusable code
  • Creating web services
  • Creating Flash Remoting elements
  • Using asynchronous CFCs
Developing structured, reusable code

CFCs provide an excellent method for developing structured applications that separate display elements from logical elements and encapsulate database queries. You can use CFCs to create application functionality that you (and others) can reuse wherever needed, like user-defined functions (UDFs) and custom tags. If you want to modify, add, or remove component functionality, you make changes in only one component file. 
CFCs have several advantages over UDFs and custom tags. These advantages, which CFCs automatically provide, include all of the following:

  • The ability to group related methods into a single component, and to group related components into a package
  • Properties that multiple methods can share
  • The This scope, a component-specific scope
  • Inheritance of component methods and properties from a base component, including the use of the Super keyword
  • Access control
  • Introspection for CFC methods, properties, and metadata
    CFCs have one characteristic that prevents them from being the automatic choice for all code reuse. It takes relatively more processing time to instantiate a CFC than to process a custom tag. In turn, it takes substantially more time to process a custom tag than to execute a user-defined function (UDF). However, after a CFC is instantiated, calling a CFC method has about the same processing overhead as an equivalent UDF. As a result, do not use CFCs in place of independent, single-purpose custom tags or UDFs. Instead, use CFCs to create bodies of related methods, particularly methods that share properties.
    For more information about UDFs, custom tags, and other ColdFusion code reuse techniques, see Creating ColdFusion Elements.
Creating web services

ColdFusion can automatically publish CFC methods as web services. To publish a CFC method as a web service, you specify the access="remote" attribute in the method's cffunction tag. ColdFusion generates all the required Web Services Description Language (WSDL) code and exports the CFC methods. For more information on creating web services in ColdFusion, see Using Web Services.

Creating Flash Remoting elements

Adobe Flash applications that use Flash Remoting can easily take advantage of ColdFusion components for business logic. In a CFC, the cffunction tag names the function and contains the application logic, and the cfreturn tag returns the result to Flash.

Note: For ColdFusion component methods to communicate with Flash applications, set the access attribute of the cffunction tag to remote.

For more information on creating CFCs for Flash Remoting, see Using Flash with CFCs.

Using asynchronous CFCs

ColdFusion provides an event gateway that lets you send a message to a CFC asynchronously. This gateway lets you initialize processing by a CFC without waiting for the CFC to complete or return a value. You can use asynchronous CFCs that use this gateway for the following:

  • Reindexing a collection
  • Logging information
  • Running batch processes
    For more information on using asynchronous CFCs, see About event gateways.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy