CFC Tutorial / ColdFusion Components By Eric Korson

Understanding Objects in ColdFusion Components

I would like to briefly discuss a few terms from the object-oriented language that you might normally use. Understanding these terms will help when working with components.

  • An object is an abstraction that is essentially a function containing variables and methods.
    In ColdFusion, we generally refer to objects as component (CFC) instances.
  • A class is a template that is used to create a new object (the new object is called an instance) and defines the variables; and, methods for all objects of a certain type.
    In ColdFusion, classes are implemented as CFCs. Individual objects are defined as instances of a particular CFC (a class).
  • In other object-orientated environments, a component has a different meaning.
    In ColdFusion, we refer to a CFCs as components.
  • A method is essentially functions that are part of an object. Methods allow the object to do something.
    In ColdFusion, CFC methods are written exactly like tag based UDFs (User Defined Functions) using the <cffunction> tag. A component is essentially a collection of related UDFs grouped within a <cfcomponent> container. For example a method can query a record from a database.
  • Properties are attributes of an object (instance variables).
    In ColdFusion components, properties are variables you set in this (public) scope or the variables un-named (private) scope. Don't confuse this type of property with the ColdFusion <cfproperty> tag. The <cfproperty> tag is used purely for declaring component metadata (data for other data). For example, my user object could have a property for the user’s first name.
  • An instance is an object of a particular class; and, the process of creating them is called instantiation.
    In ColdFusion, you get a component instance when you call a CFC using the <cfobject> tag or the createObject() function.
  • In object-oriented programming, constructors are pseudo-methods that are used to create an instance of an object. In Java, constructors are methods with the same name as the class.
    In ColdFusion, a constructor is any code contained in a CFC that is not part of a method or property. Constructor code executes in ColdFusion each time an instance of the CFC is created. You can not pass arguments to the CFCs constructor; so, the use of the constructors is fairly limited in ColdFusion. Most programmers prefer to create an init() method to act as the default constructor for their CFCs.
  • Inheritance refers to an objects ability to automatically inherit the variables and methods of its parent (super or base) class.
    In ColdFusion, this translates to a CFCs ability to inherit all methods, variables set in the constructor area, <cfproperty> metadata tag; and, anything else that CFC inherits as well.
  • Introspection is the ability for a component (or class) to examine itself.
    In ColdFusion, introspection is supported in several methods. Dreamweaver MX; and, Flash MX both have component browsers that can be used to introspect CFCs. Additionally, CFML contains a function called getMetaData() that can be used to return a structure containing all the properties, methods, arguments, return types; and, other metadata for a given CFC. ColdFusion also comes with a CFC Explorer that you can use to introspect any CFC on your server. Simply open up your browser; and, enter the URL to the CFC you would like to introspect. You should be presented with the CFC login screen.

Back To Top

Improving Performance

Using ColdFusion components increases performance because they only have to be compiled the first time they are called. After compiled once, the object remains in cache for all subsequent calls to that component; and, you can re-use the object without having to call it again in that page.

Back To Top

Instantiating a CFC (ColdFusion Component)

Typically, you create an instance of a component; and then, call a method on it, passing data in the form of arguments. There are seven different ways in which you can call a component.

1). cfinvoke tag - Instantiates a CFC and / or invokes a method on an instantiated CFC.
<cfset userObject = createObject("component", "user")>
<cfinvoke component = "#userObject#" method="getUserDetailOnId" returnVarible="usersInfo">
2). cfobject tag - Properties must be explicity set; and, methods explicitly called.
<cfobject name="userObject" component="user.cfc">
<cfinvoke component="#userObject#" method="getUserDetailOnId" returnVariable="usersInfo">
3). createObject() function - Instantiates as object via cfset, cfparam; or, cfscript. Properties must be explicitly set; and,
     methods explicitly called (See my example below: Attaching the CFC to the application scope).
      // Instantiate the User CFC Object to the application scope.
      if (NOT isDefined("application.userObj") OR isDefined("url.init")) {
      application.userObject = createObject("component", "user");
4). URL - Invokes a component method directly via HTTP GET. The CFC file name is specified in the URL along with the
     method name as a URL parameter.
5). Form Post - The Action attribute of the HTML form or cfform tag posts directly to a CFC. The method to call must be
     specified by the form field.
<form action="user.cfc" method="post">
      <input type="hidden" name="method" value="getUserDetailOnId">
      <input type="text" name="userId"> Please enter the User Id
      <input type="submit">
6). Web Service - CFCs can be consumed as web services.
      userObject = createObject("webservice", "");
      usersInfo = userObject.getUserDetailOnId(thisUsersId);
     Or assuming that you registered the web service under an alias name as "userWebService"; so, you don't have to
           remember the long wsdl path.
<cfinvoke webservice ="userWebService" method="getUserOnId" returnVarible="usersInfo">
      <cfinvokeargument name="userId" value="#thisUsersId#" />
7). Flash Remoting - Flash MX animations can call ColdFusion components via Flash Remoting.
This example could take a bit more of explaining; so, I am going to skip this one to move forward.


In my example, I created a template named cfcObject.cfm (see below). By including the template under the OnApplicationStart() method within the application.cfc, I instantiate my CFC Objects. ColdFusion will run the onApplicationStart method the next time a user requests a page.


Back To Top

Creating a CFC (ColdFusion Component)

Below I created an update method within my user component. In reality, I should have one insert (or create) method, one update method (my code below); and, one delete (or hide) method within my user.cfc below.

Working with ColdFusion Structures

First, notice below in my user.cfc that I am passing a ColdFusion structure to my updateUser method. Using this design, I can do all my updates to user table through this one component function. In my example, when I pass a structure to the SQL database, it doesn't matter in what order the fields come; or, the number of fields I pass as long as I follow the SQL rules. For example, the field names in my structure must match the field names in the database; or, I would receive a column name mismatch error.

Security for SQL Injection

Second, notice within my cfscript code, as I build the valueSet within my loop valueSet = listAppend(valueSet,key...) to pass to my SQL stored procedure, I also call my sqlSafe method (application.utilityObj.sqlSafe(userStructure[key])) to strip out any unwanted characters. The possible SQL injection is handled at the component level; and, acts as another security feature towards SQL injection (among my other security features). Once executing the cfscript tag, the variable of valueSet would look like: fname = 'Eric', lname = 'Korson' assuming the submitted form contained fname and lname; and, I entered Eric Korson.

Last, I wanted to add this method to my web service; so, I only needed to add one attribute, access="remote".


Back To Top


Back To Top

Calling the CFC (ColdFusion Component)

In my example, I created a ColdFusion structure; and, called the CFC as shown in the bottom line of code. Again, this is assumed that I submitted a form with the field names the same as my database column names. The bottom line of code calls the CFC to update the database.

Back To Top

Back To Top

Would you consider ColdFusion to be an object oriented language, why?

CFML is not an object-oriented language, why?
Objects are simply reusable application bits. They are black boxes, or magical things that do stuff, whatever you define that stuff to be. Objects often contain not just code (like CFCS) but also data, allowing data and any code that accesses it, to be cleanly encapsulated. Objects usually have multiple entry points (methods). They provide a mechanism to automatically run initialization code regardless of the entry point (a constructor). Objects can be adapted and modified, leveraging existing code without actually modifying (and potentially breaking) any of it in the process (inheritance).

For example: In ColdFusion, to perform operations on a user, you would first create an instance of the user object, and then you would be able to call a method upon it, and perhaps specifying the user ID in the process, like:


In an object-oriented world, you would have written your application very differently. You would have created a user object (a black box) that contained everything you needed for the users method. The idea is that any and all user processing would happen inside the user object. You would never access user tables directly. Actually, you wouldn't even know (or care) that the data is being stored in a table. You would just invoke methods as needed, letting the code inside the object do its thing.

This is the kind of functionality made possible using CFCs.

Back To Top