I usually var two structures. Local and retStruct. Local is where I put all
variables. retStruct would only get the things that need to be returned. At
the end, I clear local and return retStruct
With my memory, it's easier this way than remembering to var every variable
I create. And I just like to clear out the things I don't need to return.
<cfset var local {} />
<cfset var retStruct {} />
<cfset local.someConditiaonalVar = whatever />
<cfif local.someConditionalVar>
<cfhttp ... result="local.cfhttp" />
<cfquery name="local.myQry"...> select....</query>
<cfset retStruct.fileContent = local.cfhttp.fileContent />
<cfset retStruct.myQry = duplicate(local.myQry) />
</cfif>
<cfset structClear(local) />
<cfreturn retStruct />
Just don't forget to duplicate complex types when moving them to retStruct
or clearing local will clear both.
-----Original Message-----
From: Judah McAuley [mailto:[email protected]]
Sent: Thursday, March 25, 2010 12:43 AM
To: cf-talk
Subject: Re: var scope cfhttp within function
Yes, you *definitely* need to var scope implicitly created variables.
This one bit me in the ass in production as it turned out not to be
thread safe.
However, I suggest using the result attribute, not doing var cfhttp=""
as that just seems dirty to me. When you do that you are relying upon
the CF engine to assign its cfhttp variable into the one you created
by yourself and while that does work, it isn't really explicit nor is
it documented. I could see Adobe making a choice in CF10 to say, "oh,
cfhttp already exists, well I don't want to accidentally overwrite the
programmers declared variable, I'll just make a little magic scope for
my own so it doesn't conflict" and you end up with weird behavior.
Go with <cfset var myresult = "" />
<cfhttp url="foo.com" result="myresult" />
Cheers,
Judah
On Wed, Mar 24, 2010 at 6:40 PM, Dorioo <[email protected]> wrote:
>
> I believe so. Post below scoped the "result" name. Brian Kotek says in
> the comments you can also do <cfset var cfhttp="" />
>
>
http://www.coldfusionmuse.com/index.cfm/2008/6/16/cfc.application.variables
>
> - Gabriel
>
> On Wed, Mar 24, 2010 at 8:27 PM, Andrew Grosset <[email protected]>
wrote:
>>
>> using cfhttp within a function (get or head) should I be var scoping
cfhttp?
>> I'm fairly certain I should but have not been able to find any examples.
>>
>> <cffunction name="youTubedata" access="public" output="true">
>>
>> <cfargument name="youtubeKey" type="string" required="true">
>>
>> <cfset var cfhttp = structNew()>
>> <cfset var myreturn = structNew()>
>> <cfset var thisimage = false>
>> <cfset var xmlQuery = "">
>> <cfset var intRow = "">
>> <cfset var xmlRow = "">
>> <cfset var intChild = "">
>> <cfset var youtubeimage = "">
>> <cfset var youtubetitle = "">
>> <cfset var youtubeduration = "">
>>
>> <cfhttp method="get"
url="http://gdata.youtube.com/feeds/api/videos/#arguments.youtubeKey#">
>>
>> <cfif cfhttp.status_code eq 200>
>>
>> ? ? ? ?<cfset xmlQuery = XmlParse( cfhttp.filecontent )>
>>
>> ? ? ?<!--- etc, etc......... --->
>>
>> </cfif>
>>
>> </cffunction>
>>
>> Andrew.
>>
>>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know
on the House of Fusion mailing lists
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:332245
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm