Oh dear, a dodgy old piece of code in ColdFusion 8 style with local scopes!
A seldom used piece of code fell victim to the "new" ColdFusion "Local scope" from CF 9 (Centaur)
With an Application.cfc; naturally everything inside it is local'ish when the onRequest exists:
<cfcomponent displayname="Application" output="true" hint="Handle the application.">
<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false">
<cfargument name="TargetPage" type="string" required="true" />
<cfreturn true />
</cffunction>
<cffunction name="OnRequest" access="public" returntype="void" output="true">
<cfargument name="TargetPage" type="string" required="true" />
<cfinclude template="#arguments.TargetPage#" />
<cfreturn />
</cffunction>
</cfcomponent>
Suppose a simple file called ohdear.cfm
<cfset local.myvar = "abc">
included from a file here say runme.cfm:
<cfset url.myvar= "123">
<cfoutput>#myvar#</cfoutput><!--- outputs 123 --->
<cfinclude template="ohdear.cfm">
<cfdump var="#local.myvar#"><!--- outputs abc --->
<cfoutput>#myvar#</cfoutput><!--- lookup to the url scope, whoops, local is first, outputs abc, Doh! --->
So the output is: 123 abc abc
If only an Application.cfm, with no Application.cfc, then the result is completely different: 111 abc 111
Must have been a stroke of genius to think that it would be cool to use local as a scope within an include file :)
FYI; according to CF 9 and the documentation; the scope precedence is :
With an Application.cfc; naturally everything inside it is local'ish when the onRequest exists:
<cfcomponent displayname="Application" output="true" hint="Handle the application.">
<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false">
<cfargument name="TargetPage" type="string" required="true" />
<cfreturn true />
</cffunction>
<cffunction name="OnRequest" access="public" returntype="void" output="true">
<cfargument name="TargetPage" type="string" required="true" />
<cfinclude template="#arguments.TargetPage#" />
<cfreturn />
</cffunction>
</cfcomponent>
Suppose a simple file called ohdear.cfm
<cfset local.myvar = "abc">
included from a file here say runme.cfm:
<cfset url.myvar= "123">
<cfoutput>#myvar#</cfoutput><!--- outputs 123 --->
<cfinclude template="ohdear.cfm">
<cfdump var="#local.myvar#"><!--- outputs abc --->
<cfoutput>#myvar#</cfoutput><!--- lookup to the url scope, whoops, local is first, outputs abc, Doh! --->
So the output is: 123 abc abc
If only an Application.cfm, with no Application.cfc, then the result is completely different: 111 abc 111
Must have been a stroke of genius to think that it would be cool to use local as a scope within an include file :)
FYI; according to CF 9 and the documentation; the scope precedence is :
- Local (function-local, UDFs and CFCs only)
- Arguments
- Thread local (inside threads only)
- Query (not a true scope; variables in query loops)
- Thread
- Variables
- CGI
- Cffile
- URL
- Form
- Cookie
- Client
Comments
Post a Comment