You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

ColdFusion Components

A powerful tool for CF developers

I'm writing this article a few weeks before MAX takes place. I'll be one of the unfortunate few unable to join everyone for the festivities. I am keeping a close eye on the Blackstone development though, and can't wait to start writing about it in this column.

As a side note, I will be at the Powered by Detroit conference next April (mentioned in Simon's Community Column in last month's issue of CFDJ); more details on that to come (www.poweredbydetroit.org/).

This month, we're going to talk about ColdFusion Components, or CFCs as they are more commonly known. CFCs were one of the most significant features put into the ColdFusion MX release. If you haven't read last month's article on user defined functions, and are unfamiliar with them, you may want to read that before going further. User defined functions allow you to take snippets of functionality and put them into their own little package. CFCs take this concept one step further and allow you to combine data and functionality into its own little package.

The Elements of a ColdFusion Component
If you've done any development with ColdFusion, it is likely that you already know what a ".cfm" template is. You put ColdFusion code in a .cfm template and the Web server knows to send requests for that file to the ColdFusion server for processing. ColdFusion components are contained in their own file, with the extension ".cfc". The extension is how ColdFusion distinguishes the difference between ColdFusion templates (.cfm) and ColdFusion Components (.cfc).

There is more to a component than just creating a file with the extension .cfc. There are some tags involved, too. Everything in a component is wrapped in the cfcomponent tag. These are its attributes:

  • Extends: When you're dealing with inheritance, the "extends" attribute specifies the name of the component that this component will inherit its properties and methods from. This is an optional attribute.
  • Output: The output attribute specifies whether the component body is allowed to generate output. If you set this value to yes, then everything inside the component, but not in a method, is processed as if it were inside a cfoutput tag of a standard template (I'll tell you shortly about component code that's not in a method). Methods will inherit this property, but it can be overridden at the function level. If you set this to no, then the component code outside of methods is processed as if inside a cfsilent tag. That is to say, all output is suppressed (more information about cfsilent is at livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-pc7.htm#wp1103549). If you omit this attribute, then the code is processed as standard CFML. It's rare and not recommended to output data from inside of a component, so I will set this attribute to no in the examples in this article.
  • DisplayName: ColdFusion Components are self-documenting. Attempting to browse directly to a CFC is intercepted by a component browser utility that displays the component documentation on the screen. The process of generating documentation in this manner is called introspection and the data displayed is called metadata. When you view the metadata of a compoment, the DisplayName attribute is listed next to the component's name. This is an optional attribute. It has no effect on functionality.
  • Hint: The hint attribute is another attribute that helps the CFC self-documenting feature. The text specified in the hint is displayed as part of the documentation. It's optional. It has no effect on functionality.
The interesting thing about the cfcomponent tag is that it is not necessarily needed in a cfc file. A cfc file is inherently a component, so we don't need to define it as such. In reality, the cfcomponent tag is almost always used, due to its attributes such as output and extends. If you really wanted to, you could use the cfcomponent tag in a cfm file to create a component.

Inside the cfcomponent tag you can define your component methods using the cffunction tag. Each component can have as many methods as you need. More information on cffunction can be found at livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p43.htm#wp2852457. We also talked about functions in last month's column. These are the attributes to cffunction that are especially useful inside of CFCs:

  • Roles: The roles attribute is used to specify the roles that are allowed to access this function. This refers to the roles that are set when a user logs in using the ColdFusion cfloginuser tag. You can use this to control the users who can access individual component methods.
  • Access: The access attribute specifies the type of access a user must have to execute the function. The valid values for this are Private, Package, Public, and Remote. The default is Public, which means it can be executed by any local file. Private means only code inside the component can execute this function. Package means that only code inside the component package can execute it. A Package is all of the components in the same directory and subdirectories. Remote means it can be invoked remotely via Web services or Flash Remoting or (though rarely done) via URL GET or FORM POST operations.
  • Output: The output attribute on cffunction acts just like the output attribute on cfcomponent. If set to yes, then the body of the function will generate output. If set to no, then the body of the function will be treated as if it were inside a cfsilent block. In this article I'll set this to no, unless I'm trying to debug a function or if I'm creating a function whose sole purpose is to output data.
  • Displayname: Displayname is an optional attribute used only for display in the metadata of a component. Similar to the attribute of the cfcomponent tag, this attribute displays its value in parenthesis after the name of the function when viewing metadata and has no effect on performance.
  • Hint: The hint attribute is used to display comments or additional documentation in the component metadata. It is displayed under the function name as part of the metadata. It has no effect on functionality.
Other cffunction attributes, such as name and returntype still apply to functions inside a cfcomponent. The functions inside a component are often called methods, and the two terms can be used interchangeably: most developers will understand what you're talking about.

You may say: "I see that you mentioned something about code inside a component, but not inside a function. What can you tell me about that?" I'm glad you reminded me. The code in between cfcomponent tags, but not inside a cffunction block, is executed when you first create the component instance, but not when you make any subsequent calls on an existing instance. This is sometimes called "constructor" code. A constructor is an object-oriented programming (OOP)term that refers to a method that is executed when you create an object. A constructor is officially a method in OOP terminology, and since the CFC form of initialization is not a method, it is often called a pseudoconstructor.

Variables Inside a Component
There are two primary elements in a component: data and functionality. In the previous section we explored how to write the functionality. Here, you'll learn about the data. The data is stored in variables. You probably know all about variables in standard cfm templates. You can create them with cfset tags and they reside in different variable scopes, depending on the intended purpose of the variable. You can read a lot more about this in the first edition of this column (February '04).

Since you know all about variables, I'm going to jump right over the basics and go right into the scopes that exist inside a component:

  • This: This is a reference to everything public inside the component and can be thought of as the component's public scope. If you create a variable in the "this" scope, the variable can be accessed by anything that operates on the instance of the component. I don't usually use the "this" scope for variables for reasons you'll soon understand.
  • Variables: You'll recognize the variables scope from your work with cfm templates. In a component, the variables scope is used to hold private data. This data is sometimes called instance data.
  • Arguments: The arguments scope exists only inside a method, during the execution of a function. It contains all the arguments passed into the function (very similar to the "attributes" scope when creating custom tags).
  • Local function scope: The local function scope is an unnamed scope. All variables that you create with the "var" keyword using CFSET or CFSCRIPT exist in this scope.
  • Shared scopes: Inside a cfcomponent, you can access the shared scopes, such as application and session. Since the component is part of a request, request-based scopes such as URL, form, and request, are available to the component. Accessing these scopes is generally considered bad practice because it makes the code less reusable.
When developing components, I will use the "constructor" code to initialize variables in the components variables scope. I will create a number of methods to retrieve the private values (called getter methods because they only return a value) and methods to set the value of the private variables (called setter methods because they set a value).

Calling Components
There are a handful of different methods you can use to call components. I'm going to explain the one that I use most often. It's a two-step process, but is much more fun than country dancing. The first step is to create the component instance. The second step is to call various methods against the component. Component data will be persisted between component requests. Here is a sample component, entitled foo.cfc:


 <cfset variables.bar = " corner">

 <cffunction name="Getbar" access="public" returntype="string">
  <cfreturn variables.bar>

 <cffunction name="Setbar" access="public" returntype="boolean">
  <cfargument name="bar" type="string" required="Yes">
  <cfset variables.bar = arguments.bar>
  <Cfreturn true>


The foo component is very simple. It has one instance value, named bar. The bar variable is initialized to a string value "chunky".

To use the CFC you first need to create the instance of the component. You can use the cfobject tag or the createObject function to create the instance. Conceptually, both are similar. You can read about the createObject function at livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi45.htm#wp4569859. I'll explain the cfobject tag in more detail. It has two attributes in relation to components:

  • Component: The component attribute specifies the location of the component, including the name of the file without the cfc. If they are in the same directory you can just use the component name.
  • Name: The name of the return variable that will contain the component instance.
More information about using cfobject to invoke components is located at livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-pb7.htm#wp2651979. This is a snippet of code to invoke our foo component:

<cfobject component="Foo" name="objFoo">

To invoke a method on the component, we use the cfinvoke tag. These are its attributes:

  • Component: The component attribute is used to specify the name of component instance. To reference an instance that is already created, you must put it in pound signs. If you are calling a single method of a CFC without creating an instance, you can enter the location of the CFC.
  • Method: The method attribute is used to specify the name of the method you want to execute.
  • ReturnVariable: The ReturnVariable attribute specifies the name of the variable that is populated with the return value.
  • ArgumentCollection: The argument collection attribute is used to specify the name of a structure that contains all arguments that the function requires. It is optional. You can also list arguments using name value pairs that correspond to the arguments in the function, or by using cfinvokeargument tags nested inside the cfinvoke block.
After creating our instance, we can use the cfinvoke tag to check the value of the bar instance variable:

<cfinvoke component="#objFoo#" method="getBar" returnvariable="variables.Out"/>

This code calls the GetBar method, putting the return variable in the out value and then outputting the value. You'll see that the Bar instance variable has not changed since we set it. We can change that using this line of code:

<cfinvoke component="#objFoo#" method="SetBar" bar="Main"/>

Rerun the previous segment after running the Set method and you'll see that the value has correctly changed.

Are CFCs Object-Oriented?
"So, Jeff," you say, "when I create a CFC it sounds like I'm creating an object. Isn't that object-oriented programming? I've heard that the use of CFCs is object-oriented. Isn't that true?" Again, I'm glad you asked. The answer is, quite indirectly, "sort of." There is no doubt that CFCs bring many object-oriented (OO) features into the CFML language. However, just because you are using CFCs does not mean you are programming in an OO way.

When we talk about object-oriented programming or traditional (procedural) programming, we're really talking about the approach taken to design an application. In ideal cases, this application design occurs before you sit down to write code. In CF5 and before, it was not as easy to implement an object-oriented design using ColdFusion. There is a group of custom tags called cfObjects that were used to apply OO design to ColdFusion. More info is available at http://cfobjects.sourceforge.net/. ColdFusion Components make the implementation of object-oriented designs much easier.

"So, then," you ask, "CFCs are object oriented, right?" They can be, yes. But, even if you are not using an object-oriented design you can still make use of CFCs. Instead of creating classes for an OO design, you can use CFCs to create Abstract Data Types (ADTs). In simple terms, an ADT is a user-defined data type. Just like classes in OO design, Abstract Data Types provide a way to encapsulate data and the functionality to operate on that data into one component. ADTs generally do not make use of OO specific concepts, such as inheritance or polymorphism.

ColdFusion Components are a powerful tool in the toolbox of any ColdFusion developer. They provide advanced encapsulation, so that you can more easily write reusable components and speed the development of future applications. They also open the door to the use of Web services and Flash Remoting. After reading this article, you should understand all the basics of creating CFCs. Next month, I'm going to show you how I like to use CFCs and I'll step you through the process of creating a reusable component.

More Stories By Jeffry Houser

Jeffry is a technical entrepreneur with over 10 years of making the web work for you. Lately Jeffry has been cooped up in his cave building the first in a line of easy to use interface components for Flex Developers at www.flextras.com . He has a Computer Science degree from the days before business met the Internet and owns DotComIt, an Adobe Solutions Partner specializing in Rich Internet Applications. Jeffry is an Adobe Community Expert and produces The Flex Show, a podcast that includes expert interviews and screencast tutorials. Jeffry is also co-manager of the Hartford CT Adobe User Group, author of three ColdFusion books and over 30 articles, and has spoken at various events all over the US. In his spare time he is a musician, old school adventure game aficionado, and recording engineer. He also owns a Wii. You can read his blog at www.jeffryhouser.com, check out his podcast at www.theflexshow.com or check out his company at www.dot-com-it.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

@ThingsExpo Stories
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits,...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
SYS-CON Events announced today that Cedexis will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.
SYS-CON Events announced today that Google Cloud has been named “Keynote Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Companies come to Google Cloud to transform their businesses. Google Cloud’s comprehensive portfolio – from infrastructure to apps to devices – helps enterprises innovate faster, scale smarter, stay secure, and do more with data than ever before.
SYS-CON Events announced today that Vivint to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. As a leading smart home technology provider, Vivint offers home security, energy management, home automation, local cloud storage, and high-speed Internet solutions to more than one million customers throughout the United States and Canada. The end result is a smart home solution that sav...
SYS-CON Events announced today that Opsani will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Opsani is the leading provider of deployment automation systems for running and scaling traditional enterprise applications on container infrastructure.
SYS-CON Events announced today that Nirmata will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nirmata provides a comprehensive platform, for deploying, operating, and optimizing containerized applications across clouds, powered by Kubernetes. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of application containers and its underlying ...
SYS-CON Events announced today that Opsani to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Opsani is creating the next generation of automated continuous deployment tools designed specifically for containers. How is continuous deployment different from continuous integration and continuous delivery? CI/CD tools provide build and test. Continuous Deployment is the means by which...