Adobe COLDFUSION 9 Manual page 204

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
The CFC Variables scope does not include any of the Variables scope variables that are declared or available in the page
that instantiates or invokes the CFC. However, you can make the Variables scope of the page that invokes a CFC
accessible to the CFC by passing Variables as an argument to the CFC method.
You set a Variables scope variable by assigning a value to a name that has the Variables prefix or no prefix.
Values in the Variables scope last as long as the CFC instance exists, and therefore can last between calls to CFC
instance methods.
The Variables scope is available to included pages, and Variables scope variables that are declared in the included page
are available in the component page.
Note: The Variables scope is not the same as the function local scope, which makes variables private within a function.
Always define function-local variables using the var keyword of the Local scope name.
Example: sharing the Variables scope
The following example shows how to make the Variables scope of the page that invokes a CFC accessible to the CFC by
passing Variables as an argument to the CFC method. It also illustrates that the Variables scope is private to the CFC.
The following code is for the callGreetMe.cfm page:
<cfset Variables.MyName="Wilson">
<cfobject component="greetMe" name="myGreetings">
<cfoutput>
Before invoking the CFC, Variables.Myname is: #Variables.MyName#.<br>
Passing Variables scope to hello method. It returns:
#myGreetings.hello(Variables.MyName)#.<br>
After invoking the CFC, Variables.Myname is: #Variables.MyName#.<br>
</cfoutput>
<cfinvoke component="greetMe" method="VarScopeInCfc">
The following code is for the greetMe CFC:
<cfcomponent>
<cfset Variables.MyName="Tuckerman">
<cffunction name="hello">
<cfargument name="Name" Required=true>
<cfset Variables.MyName="Hello " & Arguments.Name>
<cfreturn Variables.MyName>
</cffunction>
<cffunction name="VarScopeInCfc">
<cfoutput>Within the VarScopeInCfc method, Variables.MyName is:
#variables.MyName#<br></cfoutput>
</cffunction>
</cfcomponent>
In this example, the callGreetMe.cfm page does the following:
Sets the MyName variable in its Variables scope to Wilson.
1
Displays the Variables.MyName value.
2
3
Calls the greetMe CFC and passes its Variables scope as a parameter.
Displays the value returned by the greetMe CFC.
4
Displays the Variables.MyName value.
5
6
Invokes the VarScopeInCfc method, which displays the value of Variables.MyName within the CFC.
When you browse the callGreetMe.cfm page, the following appears:
Last updated 8/5/2010
199

Advertisement

Table of Contents
loading

Table of Contents