One word: encapsulation.

The variables scope allows you to have instance data that is specific to a 
particular instantiation of an object.  So does the "this" scope, but this 
is less preferred, as it makes the properties directly available outside the 
CFC.

You cannot set or get the variables scope directly, so you need the getter 
and setter to do this.

It sounds like maybe you are talking about singletons, where you only have 
one instance of an object.  In this case you don't need getters and setters, 
since you are not setting instance data, other than perhaps a datasource 
name, which can be set the init method.

In your example below with the LastName getter and setter, you might have a 
Person class which has those getters and setters.  So you might do this:

<cfset person1 = createObject("component","Person")>
<cfset person2 = createObject("component","Person")>

<cfset person1.setLastName("Jones")>
<cfset person2.setLastName("Smith")>

<cfoutput>#person1.getLastName()#, #person2.getLastName()#</cfoutput> --  
outputs "Jones, Smith"

Now, you might want to get fancy and "do something" with the last name, for 
example remove any commas so it won't break a cfmail.  You can then do this 
in your getter:

<cffunction name="getLastName" access="public" returntype="string" 
output="false">
 <cfreturn replace(variables.lastName, ",", "", "all") />
</cffunction>

This will be done for all and any last names, and you never have to worry 
about it again.

-- Josh



----- Original Message ----- 
From: "Will Tomlinson" <[EMAIL PROTECTED]>
To: "CF-Talk" <cf-talk@houseoffusion.com>
Sent: Monday, June 23, 2008 9:46 AM
Subject: Why do I need getters/setters?


> I'm just not understanding why I'd need a getter and a setter in my cfc.
>
> My app works just fine without setting anything in the variables scope of 
> my cfcs. You submit a form, it adds the values to my db. You run a query 
> and get all the values back out.
>
> What is the value of keeping each value in its own variables scoped 
> variable? Where does it come in handy? Where are you supposed to use it?
>
> Here's a few lines from the legacy samples.
>
> <cffunction name="setLastName" access="public" output="false">
> <cfargument name="lastName" type="string" required="true" />
> <cfset variables.lastName = arguments.lastName />
> </cffunction>
> <cffunction name="getLastName" access="public" returntype="string" 
> output="false">
>  <cfreturn variables.lastName />
> </cffunction>
>
> Guess I'm missing something since I don't have an OO background. Googled a 
> little but still confused.
>
> Thanks,
> Will
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;203748912;27390454;j

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:307976
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to