I've just started learning cfcomponents as of a few days ago, and have liked
what I've seen so far, but I'm having what I think is an inheritence issue.
I have a component with three functions: an init, and then two other
functions. The init function sets a bunch of variables that I'd like to have
access to throughout the component. The other functions invoke the init
function if the init variables haven't been set yet.
In each function, I'd like to return the global vars returned by the init
function, plus a bunch of stuff unique to each function. The trouble is that
the variables from the first function call end up as part of the overall
instance data, and are then somehow getting returned in subsequent function
calls.
I'm sure it's something silly, but here's my sample code:
<cfcomponent>
<cffunction name="init" access="package" output="no">
<cfset mystruct = structNew()>
<cfset mystruct.initvar = "init varvalue">
<cfreturn this>
</cffunction>
<cffunction name="func1" access="public" output="no">
<cfset var s = structNew()>
<cfif NOT isDefined("mystruct")><cfset this.init()></cfif>
<cfset s = mystruct>
<cfset s.func1var = "value1">
<cfreturn s>
</cffunction>
<cffunction name="func2" access="public" output="no">
<cfset var t = structNew()>
<cfif NOT isDefined("mystruct")><cfset this.init()></cfif>
<cfset t = mystruct>
<cfset t.func2var = "value2">
<cfreturn t>
</cffunction>
</cfcomponent>
.....and here's how I'm invoking it:
<cfobject name="testcom" component="path.to.component">
<cfset func1output = testcom.func1()>
<cfset func2output = testcom.func2()>
<cfdump var=#func1output#>
<cfdump var=#func2output#>
What I would expect from the first cfdump is a structure containing the
"initvar" and "func1var"; and from the second cfdump a structure with "initvar"
and "func2var". However, my second cfdump call returns "initvar", "func1var"
and "func2var".
So, it would seem that the line of code where I declare this in my first
function:
<cfset s = mystruct>
is not only setting s to the value of mystruct, but also the other way around.
That is a surprise to me.
Any help is appreciated.
Thanks in advance,
Ben Mueller
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:207862
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54