Okay, thanks for the explanation and I see now that 'this' is a reference.
But it doesn't work when you try to replace an object with an older cached
version. You can do <cfset this = server.foo>, but it doesn't get returned
to the caller. I'd sure like an explanation for this behavior. I've
included test code. The cfc attempts to replace itself with an older cached
version of a former self. It seems to work- the old value of this.abc is
printed while still within the cfc. But then when the cfm page prints abc,
it reverts back to the new version somehow. Obj is not getting set to the
cached version.
Here's my test component:
<cfcomponent>
<cfset this.abc = 1>
<cfif structKeyExists(server, 'foo')>
<cfset this = server.foo> <!--- try to replace the current
object with the cached one --->
<cfset this.abc = server.foo.abc> <!--- this shouldn't be
neccessary and it doesn't help anyway --->
<cfoutput>This has been replaced with server.fooe. this.abc
= #this.abc#.<br></cfoutput>
<cfelse>
<cfset server.foo = this>
</cfif>
<cffunction name="setabc">
<cfargument name="val">
<cfset this.abc = val>
</cffunction>
</cfcomponent>
And the test .cfm page:
<cfset obj = createObject("component","com.woodward.brad.foo") /> <!--- obj
should be the cached copy once the 2nd time, but it isn't --->
<cfoutput>OBJ.ABC = #obj.abc#<br></cfoutput> <!--- this should print a 2 the
2nd time, but it prints 1 every time --->
<cfset obj2 = server.fooe> <!--- this will print a 2 the 2nd time this page
is run, because now we really do have the cached copy --->
<cfoutput>OBJ2.ABC = #obj2.abc#<br></cfoutput>
<cfset obj.setabc(2)>
-----Original Message-----
From: Sean A Corfield [mailto:[EMAIL PROTECTED]
Sent: Friday, August 15, 2003 10:35 AM
To: [EMAIL PROTECTED]
Subject: Re: [CFCDev] CFC Persistance
On Friday, Aug 15, 2003, at 07:18 US/Pacific, Brad Howerter wrote:
> Why would you return 'this' from the init function? 'This' is not a
> pointer
> to the object, so what good does it do the method caller?
Method chaining. It allows the following convenient idiom:
<cfset obj = createObject("component","mycfc").init() />
"this" *is* a reference to the object (not a pointer - there are no
pointers in Java or CF).
It can be very convenient to have all mutators (setters, initializers)
return "this" (instead of returning nothing - returntype="void"). Then
you can do:
<cfset obj.setFirstName("Sean").setLastName("Corfield") />
Also note that omitting returntype= is *not* the same as specifying
returntype="void". The latter ensures that your function returns
nothing (i.e., you have no cfreturn tag or only empty cfreturn tags).
If you simply omit the returntype= attribute, you can return anything
and it is unchecked.
Sean A Corfield -- http://www.corfield.org/blog/
"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).