Building, Extending and Calling ColdFusion Components
Posted: May 12th, 2009 | Author: Christopher Vigliotti | Filed under: ColdFusion, O-O | 5 Comments »If you don’t know what a ColdFusion component is you may want to review this before continuing.
Every system that I’ve developed over the last few years has included a Utility component. This component is where I store helper functions. The example below contains a single function that takes one argument, dumps it out on the screen then calls CFABORT.
Example 1 – Util.cfc
1 2 3 4 5 6 7 8 9 10 11 | <cfcomponent hint="contains various 'helper' functions" extends="Base"> <!--- dumpAndAbort ---> <cffunction name="dumpAndAbort" hint="call CFDUMP / CFABORT sequence from CFSCRIPT" returntype="void"> <!--- arguments ---> <cfargument name="variableToDump" type="any" required="false" default="" hint="the variable that will be displayed via CFDUMP" /> <cfdump var="#arguments.variableToDump#"> <cfabort> </cffunction> </cfcomponent> |
Now in order to use (and re-use) Util.cfc I have add it some place where I can easily access it. In the code below I’m adding an instance of Util.cfc to the Application scope.
Example 2 – Creating a new instance of Util.cfc
1 2 3 | <cfscript> Application.Util = CreateObject("component", "Util").init(); </cfscript> |
In the above code I’m using the CreateObject function and passing it two arguments. The first argument states that I’m creating a new instance of a ColdFusion component, and the second argument is the component’s name (minus the “.cfc” part). The last bit of code that you see is “.init();”. This tells the CreateObject function to call component’s “init” function. If you reexamine Example 1 you’ll notice that there is no function named “init”. WTF *
Inheritance & Psuedo-Constructors
Back at line 1 of Example 1 you’ll find the following attribute: extends=”Base”. This attribute tells ColdFusion that Util.cfc is an extension of another CFC named “Base”. That means that everything that Base.cfc can do can also be done in Util.cfc. Here’s what our Base.cfc looks like:
Example 3 – Base.cfc
1 2 3 4 5 6 7 8 | <cfcomponent hint="root-level abstract class"> <!--- init ---> <cffunction name="init" returntype="Util" hint="psuedo-constructor"> <cfscript> return this; </cfscript> </cffunction> </cfcomponent> |
Base.cfc contains a single function named init(). People will refer to this method as a pseudo-constructor. Refer to the Wikipedia entry on Constructors for a better definition of what a constructor function is.
Learn More
Once you are able to create and use CFCs you should take your newly-expanded brain over to cfoop.org.
* WTF stands for “where’s the function” or “what the function”
UPDATE 8/17/2011
cfoop.org is no longer with us. Please refer to http://www.objectorientedcoldfusion.org/ for more CFOOPY goodness.
What is the point of an init() function if the result is exactly the same as without the init()?
Good question! In the above example there is no point…but in situations where you would want to set variables and/or perform some logic when you create a new instance of an object having an init() method can be helpful.
To make it more “constructor like” you should do:
cfcomponent
cfset init()
cffunction name=”init”
Constructor never returns any value.
I’m not certain, but I don’t think that the above code will work in ColdFusion. If I’m missing something please post a link to a code sample somewhere. Note that I did use the term ” Psuedo-Constructor” in my post.
Also, thanks for your comments!
[...] This is a nice tutorial on CFCs and extending them if you are a bit rusty or if this concept is new. [...]