No. not really.

When you "var" a variable you do, indeed, make it private to the function.
However if you place a reference to a public object in a var'd variable it's
still a public object.

In a sense the var keyword only affects the actual label of the variable,
not its contents.  It does not make clones of its contents or anything like
that.

The following is bad code, but (I hope!) should explain things:

<cfset foo = structnew() /> <!--- this creates a variable "foo" in the
variables scope.

<cfset foo.fiddle = 3 />

<cffunction>

<cfset var foo =  structnew() />  <!--- this creates a brand new variable
called "foo" completely unconnected with the one in the variables scope.

</cffunction>



<cffunction>

<cfset var foo =  variables.foo />  <!--- this creates a brand new variable
called "foo" completely unconnected with the one in the variables scope.
HOWEVER the value in this completely unconnected variable is a reference to
the variables.foo varialble.  Changing foo.fiddle in the function changes
foo.fiddle in the referenced object.

</cffunction>



Does that make sense or muddy things further?

Jim Davis



From: Michael Hodgdon [mailto:[EMAIL PROTECTED]
Sent: Wednesday, September 08, 2004 3:05 PM
To: CF-Talk
Subject: reference in cfc

Question for the list.  It has to do with what appears to be object
reference
with data stored in a cfc.  Any help you got would be great, oh yeah, and
kind
of an emergency fo us!  Coming up on a deadline fast!

It is my understanding that when you "<cfset var var_myvar = "">" (var out a

variable) you capture information at a function level not at a component
level.  
Therefore if I have a data collection in a component that I want to grab,
analyze, edit, and send to the user without editing the original shared
component version I can just grab it using the var scope.  Once in the var
scope
I can make my edits without affecting the original shared scope version.
Maybe
a description will better explain this:

Here is the code I am using to access and modify my component:

<!--- create and initialize the test object in local memory --->
<cfset variables.test = createObject("component", "cf_components.test")>
<cfset variables.test.initializeObject()>

<!--- get and dump the data for this customer  --->
<!--- you should see two keys "firstname" and "lastname" --->
<cfdump var="#variables.test.getCustData()#">

<!--- perform a customer analysis, this customer analysis should return a
structure with a new "description" key in it --->
<!--- Inside the function I have scooped the component level customer
information by using a getter on the original shared data --->
<!--- Thre returnvariable dumps the data into the var scope.  My edits
should
remain only in that vared out variable --->
<!--- with the "firstname", "lastname", and "description" field contained in

var_stCustomerData --->
<cfdump var="#variables.test.customerAnalysis()#">

<!--- get the customers original data.  This data sits in the shared scope
of
the test object and should not reflect the changes made in customerAnalysis
to
the vared version --->
<!--- I should only see two keys "firstname" and "lastname" however the edit

performed in customerAnalysis (on the vared version of the data) edits the
original information putting description in the shared component version
--->
<cfdump var="#variables.test.getCustData()#">

Here is my cfc:
<cfcomponent displayname="test" hint="I am a component that test references
to
stored data.">
  
<cffunction name="initializeObject" hint="I prepare the object by setting
some
intial data" access="public" output="no" returntype="boolean">

  <!--- Set up the customer object --->
  <cfscript>
   
  this.objName = "customer";
   
  // Set up the customer object data containers
  stCustomerInfo        = structNew();
  stCustomerInfo.firstname     = "michael";
  stCustomerInfo.lastname      = "hodgdon";

  </cfscript>   

  <cfreturn "1">
  
</cffunction>

<cffunction name="getCustData" hint="I return the customer data"
access="public" output="no" returntype="struct">
  
  <cfreturn stCustomerInfo>  
  
</cffunction>

<cffunction name="customerAnalysis" access="public" output="no"
returntype="any">
  
  <cfset var var_stCustomerData = "">

  <!--- grab the customer data and place it in a local variable --->
  <cfinvoke
   method="getCustData"
   returnvariable="var_stCustomerData">
  
  
  <!--- if your name is michael --->
  <cfif var_stCustomerData.firstname EQ "michael">
   
   <!--- add to the temp local var a customer description --->
   <cfset structInsert(var_stCustomerData, "description", "Your name is
mike")>
   
   
  <!--- your name is not michael  --->
  <cfelse>
  
   <!--- add to the temp local var a customer description --->
   <cfset structInsert(var_stCustomerData, "description", "Your name is not
mike")>   
   
  </cfif>
  
  <!--- return the edited customer information --->
  <cfreturn var_stCustomerData>  
  
</cffunction>

</cfcomponent>

  _____
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to