Elements of a CFC
A CFC definition has the following characteristics:
•
It is a single CFML page with a .cfc file extension. The component name is the same as the
filename. For example, if the file is myComponent.cfc, the component name is
myComponent.
•
The page is surrounded by a
•
The component page defines methods (functions), properties (data), or both. Most CFCs have
methods or methods and properties, but you can also have a CFC that contains only
properties.
•
You use the
cffunction
create simple methods, but it does not provide options to control access to the method, provide
metadata, specify a return type, or control generated output.
•
Like other ColdFusion functions, CFC methods can display information directly by
generating output, or can return a value to the code or client that invoked the method.
•
Any code that is outside of
This code executes each time you invoke a CFC method transiently, without using the
tag or
cfobject
CreateObject
The following is a simple CFC definition with one property, the
method,
helloSubject
<cfcomponent>
<cfset subject="world">
<cffunction name="helloSubject">
<cfreturn "Hello " & subject>
</cffunction>
</cfcomponent>
CFC invocation and instantiation
There are two ways to use a CFC:
•
You can invoke (call) a method of the CFC transiently. In this case, you do not create an
instance of the CFC object that you can reuse elsewhere in your CFML. Instead, the CFC
instance exists only from the time you invoke the method until the method returns. No data is
preserved between invocations. Code that is outside all
each transient invocation.
Invoke a CFC method transiently if your CFML request uses the CFC only once. Also, if you
transiently invoke a CFC method frequently, consider creating a user defined function to
replace the CFC method.
•
You can instantiate a CFC object. This is called instantiating the CFC or creating a CFC
instance. You then invoke the methods of the instance. You can access the CFC methods and
data as instance elements. You can also use the instance in the
methods. When you instantiate a CFC, data in the CFC is preserved as long as the CFC
instance exists, and ColdFusion does not incur the overhead of creating the instance each time
you call a method.
Instantiate CFCs to preserve data in the CFC. To ensure processing efficiency if you use the
CFC more than once on a page, instantiate the CFC before you invoke its methods.
cfcomponent
tag to define CFC methods. The CFScript
definitions executes when the CFC is instantiated.
cffunction
function to create a CFC instance.
:
tag. No code can be outside this tag.
subject
cffunction
cfinvoke
About ColdFusion components
statement can
function
variable, and one
definitions executes at
tag to invoke the CFC
223
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers