Reading a value from a session variable into a local variable in Coldfusion -


i hope can me. read session variables local variable, don't have create endless read locks. came across interesting behavior. note have not applied write locks brevity's sake:

consider following:


example 1:

<cfset session.testvalue = 1 /> <cfset lcktestvalue = session.testvalue /> <cfoutput>#lcktestvalue#</cfoutput><br /> <cfset session.testvalue = 2 /> <cfoutput>#lcktestvalue#</cfoutput> 

output:

1
1


example 2:

<cfset session.testvalue1.item = 1 /> <cfset lcktestvalue1 = session.testvalue1 /> <cfoutput>#lcktestvalue1.item#</cfoutput><br /> <cfset session.testvalue1.item = 2 /> <cfoutput>#lcktestvalue1.item#</cfoutput> 

output:

1
2


i trying work out why second example, updates 'lcktestvalue1.item', when value read once? have expected example 1 & 2 produce same output, , following produce second example's output:


example 3:

<cfset session.testvalue1.item = 1 /> <cfset lcktestvalue1 = session.testvalue1 /> <cfoutput>#lcktestvalue1.item#</cfoutput><br /> <cfset session.testvalue1.item = 2 /> <cfset lcktestvalue1 = session.testvalue1 /> <cfoutput>#lcktestvalue1.item#</cfoutput> 

output:

1
2


the reason behavior, can think of, second example, uses structure inside structure. but,i cannot expand on concept. can you? need understand this, because creating shopping cart, makes extensive use of methodology in example 2. works fine, not sure why, , afraid under load, may fail?

thank can give me on this.

charles, is expected behavior. structures , objects passed by reference - meaning setting pointer memory location @ underlying object. when set statement dealing primitive (a string, number, date etc) copies data new variable - this:

<cfset session.testvalue = 1/> <cfset testvalue = session.testvalue/> 

...actually creates 2 locations each containing "1"

when dealing object (a structure, component, function, query etc. - not primitive), this:

<cfset session.testvalue.item1 = 1/> <cfset testvalue = session.testvalue/> 

the variables.testvalue variable pointer structure created in set statement (session.testvalue.item1 implicitely creates structure).

this important fundamental stuff - when dealing cfcs, functions, application scopes etc. post on primitive vs complex variables explains in more detail.

it has nothing locking way. unless have demonstratable race condition need not go overbord locking.

i note practice here not great idea - should scoping local vars variables.varname way in case pushing session variables scope not saving effort.

note: while writing ray chimed in short concise answer point (while being verbose).

note 2: in coldfusion there 1 nuance find unexpected - arrays passed value (as copy) , not reference. can throw - , if ever array of objects


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -