Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Coldfusion Components

Coldfusion components are a means for Coldfusion developers to package related functionality into a single package, class or object. While coldfusion is not an object oriented language it provides certain means for developers to develop in an OO way, in this way the coldfusion component is an object oriented metaphor.

Typically a CFC (coldfusion component template) is made up of a collection of related functions or functions that all have to do with a certain theme. An example of this would be a CFC called User.cfc. This component would then contain all the functions dealing with users of a system e.g. Creating, Reading, Updating, Deleting etc.

Like most other programming languages Coldfusion Allows you to create functions that are private to the component, meaning only functions or methods of the component can call the private method and not from outside of the method.

Creating a Component

To create a Coldfusion Component you start with the cfcomponent tag, and then add functions using the cffunction tag. Below is an exmaple of a simple USER component that contains two public methods Create and Delete. The logic inside the two functions has been left out.
<cfcomponent output="no" displayname="user">

<cffunction name="create" access="public" returntype="boolean">
   <cfargument name="userName" required="yes" type="string">
   <cfargument name="password" required="yes" type="string">
   <cfargument name="email" required="yes" type="sring">
   <cfset successful = false>
<!--- create the user --->
   <cfreturn successful>
</cffunction>

<cffunction name="delete" access="public" returntype="boolean">
   <cfargument name="userid" required="yes" type="numeric">
   <cfset successful = false>
<!--- delete the user --->
   <cfreturn successful>
</cffunction>

</cfcomponent>

Instantiating a Component

Once you have created your component you need to be able to access the methods inside the component, you do this by instantiation. Like most things in Coldfusion you have several different ways to instantiate your component.
  • Using the cfinvoke tag
  • Using the cfobject tag
  • Or using the shortcut createObject function in cfscript

cfInvoke

When using the cfinvoke tag to call a method in a component the cfinvoke tag automatically instantiates the component then invokes the method in the component all in one go. The code looks like:
<cfinvoke component="user" method="create" returnvariable="success">
<cfinvokeargument name="username" value="ggilbert"/>
<cfinvokeargument name="password" value="password"/>
<cfinvokeargument name="email" value="emailme@myemail.com"/>
</cfinvoke>

cfObject

The cfobject tag is a little different than the cfinvoke tag in that the cfobject tag only instantitates the component and then returns a instance of the object allowing you access to all the public methods within the component. You would use the cfobject tag like so:
<cfobject name="userObj" component="user">
<cfset bSuccessful = userObj.create("ggilbert","password","myemail@myeamil.com")/>
You could also have used a cfinvoke after the create object but I find it to be a bit too much typing for my liking.