User Guide

Table Of Contents
220 Chapter 10: Building and Using ColdFusion Components
2.
Create a ColdFusion page, with the following code, and save it in the same directory as the
tellTime component:
<h3>Time Display Page</h3>
<b>Server's Local Time:</b>
<cfinvoke component="tellTime" method="getLocalTime">
Using the cfinvoke tag, the example invokes the getLocalTime component method without
creating a persistent CFC instance.
Using the cfinvoke tag within the CFC definition
You can use the
cfinvoke tag to invoke a component method within the component definition;
for example, to call a utility method that provides a service to other methods in the component.
To use the
cfinvoke tag in this instance, do not create an instance or specify the component
name in the
cfinvoke tag, as the following example shows:
<cfcomponent>
<cffunction name="servicemethod" access="public">
<cfoutput>At your service...<br></cfoutput>
</cffunction>
<cffunction name="mymethod" access="public">
<cfoutput>We're in mymethod.<br></cfoutput>
<!--- Invoke a method in this CFC. --->
<cfinvoke method="servicemethod">
</cffunction>
</cfcomponent>
Note: When you invoke a method from within the component definition in which you define the
method, do not use the This scope, because this resets the access privileges.
Invoking methods using dynamic method names
The
cfinvoke tag is the only way to efficiently invoke different component methods based on
variable data (for example, form input). In this case, you use a variable name, such as
Form.method, as the value of the
method attribute. In the following example, the user selects a
report from a form:
<select name="whichreport">
<option value="all">Complete Report</option>
<option value="salary">Salary Information</option>
</select>
The cfinvoke tag then invokes the appropriate method, based on what the user selected:
<cfinvoke component="getdata" method="#form.whichreport#"
returnvariable="queryall">
Using components directly in CFScript and CFML
You can invoke methods of a component instance directly using CFScript or in CFML tags. To
invoke component methods directly, use the
CreateObject function or cfobject tag to
instantiate the component. Thereafter, use the instance name followed by a period and the
method that you are calling to invoke an instance of the method. You must always use parentheses
after the method name, even if the method does not take any parameters.