Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
vollja #1
return object instances
hello,
since I'm new to the world of object oriented programming I hope I use the
right terms for the concerned things, but I'll try.
I was wondering if returning instances of cfobjects as a return value of a
method.
What I did is create in a method of an object A an instance of another object
B of a different type. Now I want to return that instance of A via <cfreturn>.
I have seen that with other data-objects like query-objects there is a way of
packing the data-object in an array, return the array and access the returned
object from the returned array. This WORKS!
But I run into another Problem. Later in the code I create an instance C of
the same type as B. As soon as I set properties on instance C, instance B is
changed and gets all properties from instance C although instance B is never
touched after being returned with the array.
Oh, and I forgot to mention, the problem arises not always but depending on
what has been done to instance B inside the method of A.
All that happens inside a component that itself is used as a web service.
The underlying code is quite long and cryptic and I haven't created an example
code with the same problem. Sorry about that. The real question is if what I'm
doing is something one isn't supposed to do, if it is actually an error in the
object-handling of CF 6.1 (which I'm using).
Thanks
Volker
vollja Guest
-
Multiple app instances using same shared object.
yeah you can Connect SharedObjects between instances of the same app and even instances of two diffrent apps. you need to use proxied sharedObjects... -
Object property with different return types
I have a control 'MyControl' with a property of type 'MyObject'. MyObject has a property 'MyField' that is a enum type. I want the type of enum... -
Return object
(Inspired by Test Driven Development of Kent Bek) How Can I return from a class method object of the class ? Like this: class Dollar attr_reader... -
Object Instances
Another source told me a reason I'm goofing on my program (see 'Absurdly Easy Cocoa Question') is that I'm not calling instance methods on object... -
Object Instances: Never Mind
Bleh, I'm a twit. After last post but before Mr. Ash's, I figured out my problem. I was referencing a class (ThermCalc is its name), not an outlet... -
mpwoodward *TMM* #2
Re: return object instances
vollja wrote:
You'd really have to post some code for us to see what you're> hello,
>
> since I'm new to the world of object oriented programming I hope I use the
> right terms for the concerned things, but I'll try.
> I was wondering if returning instances of cfobjects as a return value of a
> method.
> What I did is create in a method of an object A an instance of another object
> B of a different type. Now I want to return that instance of A via <cfreturn>.
> I have seen that with other data-objects like query-objects there is a way of
> packing the data-object in an array, return the array and access the returned
> object from the returned array. This WORKS!
> But I run into another Problem. Later in the code I create an instance C of
> the same type as B. As soon as I set properties on instance C, instance B is
> changed and gets all properties from instance C although instance B is never
> touched after being returned with the array.
> Oh, and I forgot to mention, the problem arises not always but depending on
> what has been done to instance B inside the method of A.
> All that happens inside a component that itself is used as a web service.
> The underlying code is quite long and cryptic and I haven't created an example
> code with the same problem. Sorry about that. The real question is if what I'm
> doing is something one isn't supposed to do, if it is actually an error in the
> object-handling of CF 6.1 (which I'm using).
>
> Thanks
> Volker
>
doing--based on what you're describing, however, I bet CF is behaving as
it's supposed to and you're inadvertently doing things that are causing
unexpected side effects.
Matt
--
Matt Woodward
[email]mpwoodward@gmail.com[/email]
Team Macromedia - ColdFusion
mpwoodward *TMM* Guest
-
vollja #3
Re: return object instances
Hi,
here is some code. I haven't tried this one yet. Though I doubt it will
actually show the effect. The original code has alltogether about 3000 lines,
gets XML data as parameter of a web service and feeds a database. I doubt,
anyone would be willing to read it!
Volker
<cfcomponent output="yes">
<cffunction name="parent" access="public" returntype="string">
<cfreturn 'not interesting'>
<cfobject name="instA" component="A">
<cfobject name="instC" component="B">
<cfset arr_result = instA.foo()>
<!--- So far everything all right --->
<cfdump var="#arr_result#">
<cfset instC.propB = 2>
<!--- Now the result looks different --->
<cfdump var="#arr_result#">
<cfreturn 'not interesting'>
</cffunction>
</cfcomponent>
<!--- Component A --->
<cfcomponent output="yes">
<cfset this.propA = 0>
<cffunction name="foo" output="true" returntype="array">
<cfobject name="instB" component="B">
<cfset instB.propB = 1>
<cfset array = arraynew(1)>
<cfset result = arrayappend(array, instB)>
<cfreturn array>
</cffunction>
</cfcomponent>
<!--- Component B --->
<cfcomponent output="yes">
<cfset this.propB = 0>
</cfcomponent>
vollja Guest
-
mpwoodward *TMM* #4
Re: return object instances
vollja wrote:
Given the fact that there is nothing in this code (that I can see) that> Hi,
>
> here is some code. I haven't tried this one yet. Though I doubt it will
> actually show the effect. The original code has alltogether about 3000 lines,
> gets XML data as parameter of a web service and feeds a database. I doubt,
> anyone would be willing to read it!
>
> Volker
>
>
> <cfcomponent output="yes">
> <cffunction name="parent" access="public" returntype="string">
> <cfreturn 'not interesting'>
> <cfobject name="instA" component="A">
> <cfobject name="instC" component="B">
> <cfset arr_result = instA.foo()>
> <!--- So far everything all right --->
> <cfdump var="#arr_result#">
> <cfset instC.propB = 2>
> <!--- Now the result looks different --->
> <cfdump var="#arr_result#">
> <cfreturn 'not interesting'>
> </cffunction>
> </cfcomponent>
>
> <!--- Component A --->
> <cfcomponent output="yes">
> <cfset this.propA = 0>
> <cffunction name="foo" output="true" returntype="array">
> <cfobject name="instB" component="B">
> <cfset instB.propB = 1>
> <cfset array = arraynew(1)>
> <cfset result = arrayappend(array, instB)>
> <cfreturn array>
> </cffunction>
> </cfcomponent>
>
> <!--- Component B --->
> <cfcomponent output="yes">
> <cfset this.propB = 0>
> </cfcomponent>
>
changes arr_result between the first dump and the second one, either
there's something going on that isn't illustrated here or the variable
actually doesn't get changed. The call to instA.foo() sets arr_result.
Then you do instC.propB = 2. That has *nothing* to do with arr_result
from what I can see here, so I don't see how the next time you output it
it would be different.
Matt
--
Matt Woodward
[email]mpwoodward@gmail.com[/email]
Team Macromedia - ColdFusion
mpwoodward *TMM* Guest
-
vollja #5
Re: return object instances
Matt wrote:
well, that's exactly what I was wondering about as well. Everything is o.k.>Given the fact that there is nothing in this code (that I can see) that
>changes arr_result between the first dump and the second one, either
>there's something going on that isn't illustrated here or the variable
>actually doesn't get changed. The call to instA.foo() sets arr_result.
> Then you do instC.propB = 2. That has *nothing* to do with arr_result
>from what I can see here, so I don't see how the next time you output it
>it would be different.
>
>Matt
until something completely different happens which should not really interfere.
Volker
vollja Guest
-
BSterner #6
Re: return object instances
Remember that you have 3 possible variable scopes from w/i your object.
1) 'var' = private to function
2) 'variables' = private to component
3) 'this' = public to all
Use the 'var' scope if nothing outside the function needs access to the
variable. Use 'variables' if only other functions w/i the component need
access. Use 'this'...well...um, I don't ever use it.
eg) <cffunction name="parent" access="public" returntype="string">
<!--- Only variables w/i my function can access this --->
<cfset var instC = createObject("component", "A")>
<!--- Only variables w/i my component can access this --->
<cfset variables.instC = createObject("component", "A")>
<!--- Everybody can access this --->
<cfset this.instC = createObject("component", "A")>
</cffunction>
Scoping all variables is a good way to code and helps prevent you from
inadvertenely making mistakes.
BSterner Guest
-
BSterner #7
Re: return object instances
I know there were some bugs w/the 'variables' scope and also using 'var' and then <cfinclude>, but from what I've read they were fixed in the newer versions of CF.
BSterner Guest
-
mpwoodward *TMM* #8
Re: return object instances
BSterner wrote:
Good points--I didn't want to get the discussion to get too complex too> Remember that you have 3 possible variable scopes from w/i your object.
>
> 1) 'var' = private to function
> 2) 'variables' = private to component
> 3) 'this' = public to all
>
> Use the 'var' scope if nothing outside the function needs access to the
> variable. Use 'variables' if only other functions w/i the component need
> access. Use 'this'...well...um, I don't ever use it.
quickly, but that actually is the first thing that came to my mind as
well. You need to var scope EVERYTHING inside your CFC functions, and
I'm with BSterner, I never use the this scope for anything.
Matt
--
Matt Woodward
[email]mpwoodward@gmail.com[/email]
Team Macromedia - ColdFusion
mpwoodward *TMM* Guest



Reply With Quote

