Strange behavior of cold arguments left out of function?

I have been programming ColdFusion for over 15 years, but have never met this. Here is the code for replicating behavior:

<cfset _run()> <cffunction name="_run"> <cfset variables.dataArray=ArrayNew(1)> <cfset local.data={ area="profile" }> <cfset _append(data=local.data,field="name")> <cfset _append(data=local.data,field="phone")> <cfdump var="#variables.dataArray#" label="dataArray"> </cffunction> <cffunction name="_append"> <cfargument name="data" type="struct" required="yes"> <cfargument name="field" type="string" required="yes"> <cfdump var="#arguments#" label="arguments"> <cfset arguments.data.field=arguments.field> <cfset ArrayAppend(variables.dataArray,arguments.data)> </cffunction> 

As you can see, this is what I am doing:

  • Inclusion of an array in a variable region to make it accessible to the whole world
  • Entering a structure (local.data) in the local area
  • Adding the first field element (name) by calling the _append function data
  • Adding a second field element (phone) in the same way

This code will lead to the following output:

Output dump

As you can see, the code leads to an array with duplicate entries, when you can expect the first index to have a field = "name". As you can also see, the data value that is called for _append a second time contains the "field" property with the value "name". Does it seem to linger in the argument field the first time we called a function? How is this possible. I thought the argument area was isolated inside the cffunction tag?

But if I replaced the _append function as follows:

 <cffunction name="_append"> <cfargument name="data" type="struct" required="yes"> <cfargument name="field" type="string" required="yes"> <cfdump var="#arguments#" label="arguments"> <cfset local.data=Duplicate(arguments.data)> <cfset local.data.field=arguments.field> <cfset ArrayAppend(variables.dataArray,local.data)> </cffunction> 

he will give the following result:

Dump Output 2

As you can see, duplicating the arguments. data before adding "fields" to it, solves the problem. Note that it is simple:

 <cfset local.data=arguments.data> 

was not enough.

Can someone explain this behavior of the argument field?

+5
source share
1 answer

So, after some research, I found this on the Adobe Coldfusion documentation page (text offset made by me):

About the Arguments area

All arguments to the function exist in their scope, the scope of the Arguments. The Argument area exists for the life of a function call. When the function returns, the scope and its variables are destroyed. However, destroying the Argument area does not destroy the variables, such as structures or query objects, that ColdFusion passes to the function by reference. The variables on the calling page that you use as arguments to the function continue to exist; if the function changes the value of the argument, the variable on the calling page reflects the changed value .

It was an opening for me to see, and it will save me from trouble in the future :)

+4
source

Source: https://habr.com/ru/post/1274096/


All Articles