Tom Muck's Blog: CFC Instance copies fail
News and Views
CFC Instance copies fail
Saturday, March 11, 2006 7:14:50 PM
I discovered an interesting thing about CFCs that I did not know, and the cause of an error message I have been having problems resolving. I have several CFCs that I use in session or application variables. I made the mistake of using StructCopy to put an instance of the application variable instance into request scope (Duplicate does not seem work on CFCs). Most things seemed to work perfectly, but when attempting to use a function within the CFC, there did not seem to be a way to do it:
<cfcomponent name="test">
<cffunction name="foo">
<cfreturn "blah"/>
</cffunction>
<cffunction name="callFoo">
<cfset var temp = foo()>
<cfreturn temp />
</cffunction>
</cfcomponent>
Because callFoo() calls foo() within the cfc, it fails. This works:
<cfset application.test = CreateObject("component", "test")>
<cfoutput>#application.test.foo()#</cfoutput>
This works:
<cfset application.test = CreateObject("component", "test")>
<cfoutput>#application.test.callFoo()#</cfoutput>
This even works:
<cfset application.test = CreateObject("component", "test")>
<cfset request.test = StructCopy(application.test)>
<cfoutput>#request.test.foo()#</cfoutput>
But this fails:
<cfset application.test = CreateObject("component", "test")>
<cfset request.test = StructCopy(application.test)>
<cfoutput>#request.test.callFoo()#</cfoutput>
You get the following error message:
Variable foo is undefined.
Also, if you use this scope inside the cfc, it will fail:
<cfcomponent name="test">
<cffunction name="foo">
<cfreturn "blah"/>
</cffunction>
<cffunction name="callFoo">
<cfset temp = this.foo()>
<cfreturn temp />
</cffunction>
</cfcomponent>
It works until you try to copy the CFC, after which you get this error:
Variable THIS is undefined.
Can anyone shed any light on why everything within the copy works except for local functions or the scope "this"?
Category tags: Dreamweaver, ColdFusion
Posted by Tom Muck
Add comment |
View comments (3) |
Permalink
|
Trackbacks (0)
|
Digg This
Before posting comments or trackbacks, please read the posting policy.