Calendar

««Mar 2010»»
SMTWTFS
  123456
7
8
910111213
14151617181920
21222324252627
28293031

Alert Email

Get a short email alert whenever a new entry is published.

Confidential, secure it's piece of cake to keep uptodate.

Q: What's new with CFML? A: CFC's

Published: 7:06 AM GMT, Friday, 13 June 2008

When you are at the bleading edge all the time, you sometimes forget that maybe not everyone is up to speed with the latest developments. As we strive to provide the best engine environment for CFML developers we are always reaching to enhance and roundout the language. But we mustn't forget to keep selling the current cool features of CFML as new people come on board and current CFML developers branch out.

We are often asked by new CFML people, so what's the latest? You ask them what they already know and they will more often than not retort they haven't touched CFML since v5 or even as far back as v4.5. A lot of catching up to do! Of course there is lots of cool things that have happenned since then, but one of the biggest advances is still the humble CFML Object, the CFC.

Seifenblase (Bulle de Savon, Soap Bubble)

Even CFML conference sessions usually have an introduction to using objects within CFML and how CFC's can really boost your productivity. There is obviously a hunger for this information. Our very own Matt Woodward gave an excellent talk on Objects at this years Scotch conference.

So what is a CFC (ColdFusionComponent)?

A CFC is simply CFML's way of representing an object. An object, just like any other language, can have data and methods to which to act upon that data. Methods and data can be limited as to who can and cannot access them, using the standard 'public' / 'private' directives found in most OO languages. It can also inherit from other objects, adding or masking functionality as required.

Just like any other language, to make your object live you first of all have to create an instance of it and after that you can then act upon the methods and data. This object can now exist as part of your CFML application and be passed around just like any other CFML data type. In OpenBD, all CFCs are serializable, which in short means you can put them into a session scope and they will be replicated across your cluster.

<cfcomponent>
 
 <cfset variables.name = "">

 <cffunction name="setName" returntype="void">
  <cfargument name="_name" required="true">
  <cfset variables.name = _name>
 </cffunction>

 <cffunction name="getName" returntype="string">
  <cfreturn variables.name>
 </cffunction>

</cfcomponent>

People will sometimes draw parallels with custom tags and CFCs. This only works up to a given point. Both techniques allow you to build your application to reuse code. For example, you may have a tag that cleans up a string by removing all the HTML tags. This could be accomplished in either a CFC or a custom tag. In many respects, a custom tag is a very basic singleton object, except it can't actually retain any data.

So if you are familiar and comfortable creating your custom tags, then you are already half way to using CFC's.

One of the really cool features of a CFC is that they are automatically accessible via the standard SOAP web service call. The CFML engine will automatically create a WSDL allowing you to remotely operate with the object. Anyone that has struggled with providing SOAP endpoints in Java or any other language, will love that this facility comes free-of-charge with no effort required from the CFML developer.

There are no limitations as to what you can do within a CFC; anything you would have put in a normal CFML page then you can do in a CFC.

If you haven't checked out the power of CFC's then we would urge you to give them a look. Once you have started to play with them you'll wonder how you ever managed without them. Start small and build up.

For more detailed information on CFC's be sure to check out an excellent primer at O'Reilly.

Comments (0)

Add Comment