Creating A VARIABLES-Scoped Function Of The Same Name

Posted March 12, 2007 at 4:08 PM

Tags: ColdFusion

I have 5 minute before my lunch break is over and I just wanted to try this (not very exciting at all). What happens when you have a VARIABLES-scoped user defined ColdFusion function that has the same name as a built in ColdFusion function. The test:

 Launch code in new window » Download code as text file »

  • <!---
  • Create a very simple UDF that simply echoes
  • back the first argument that it is passed.
  • --->
  • <cffunction name="Echo">
  • <cfreturn ARGUMENTS[ 1 ] />
  • </cffunction>
  •  
  •  
  • <!--- Assign this UDF to the "Find" key within variables. --->
  • <cfset VARIABLES.Find = VARIABLES.Echo />
  •  
  • <!--- Now, try to call this both with and without a scope. --->
  • #VARIABLES.Find(
  • "Naughty",
  • "Naughty girl!"
  • )#
  •  
  • #Find(
  • "Naughty",
  • "Naughty girl!"
  • )#

Running the above code, we get the output:

Naughty
1

Calling the UDF with the original VARIABLES scope calls our UDF. Calling it without the scope invokes the built-in ColdFusion method. Interesting.

Ok, what about if we do the same thing as above, but this time, we do NOT use the VARIABLES scope at all:

 Launch code in new window » Download code as text file »

  • <!---
  • Set the Echo method directly into the non-scoped
  • Find variable.
  • --->
  • <cfset Find = VARIABLES.Echo />
  •  
  • <!--- Now, try to call this both with and without a scope. --->
  • #VARIABLES.Find(
  • "Naughty",
  • "Naughty girl!"
  • )#
  •  
  • #Find(
  • "Naughty",
  • "Naughty girl!"
  • )#

Running the above, we get the output:

Naughty
1

Exactly the same effect whether or not we use the VARIABLES scope. Kind of interesting. Nothing really important here. The only strange thing is that if you do this:

 Launch code in new window » Download code as text file »

  • <cfdump var="#GetMetaData( Find )#" />

It gives you the UDF meta data and NOT the built in ColdFusion method meta data.

Ok, back to work :)

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Mar 13, 2007 at 10:26 AM // reply »
100 Comments

Presumably if you do getmetadata( variables.find ) you get the data you expect?


Mar 14, 2007 at 2:06 PM // reply »
1 Comments

Ben,

Super cool tip. It also looks like a good way to play a practicable joke on a CF developer.

David.


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 8:09 AM
Creating A "Remember Me" Login System In ColdFusion
@Nikos, Heck yeah! Glad you got things working smoothly. ... read »
Feb 9, 2010 at 8:05 AM
Creating A "Remember Me" Login System In ColdFusion
No probs :) anyway , Im good now :) ... read »
Feb 9, 2010 at 8:02 AM
Creating A "Remember Me" Login System In ColdFusion
@Nikos, I've seen people use the J2EE sessions, but I have not used them myself... yet. ... read »
Feb 9, 2010 at 7:57 AM
Ask Ben: Converting a Query to an Array
@Stju, Did you actually test this? I ask because there is a fatal flaw in it - you are using the same Row struct for every row. Since Structs are passed by reference, every subsequent update you ma ... read »
Feb 9, 2010 at 7:50 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
@Thomas, Not bad. I suppose you could do the same with Top as well as margin. ... read »
Feb 9, 2010 at 7:47 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
@Johan, I don't think I have one off hand. Basically, you'd just want to use the File attribute of CFDocument to save the PDF to disk. Then, you'd want to use the File attribute of CFMailParam to a ... read »
Feb 9, 2010 at 5:34 AM
Creating A "Remember Me" Login System In ColdFusion
Any change you could show how to take advantage how the J2ee session stuff in your code? http://kb2.adobe.com/cps/182/tn_18232.html ... read »
Feb 9, 2010 at 5:32 AM
Creating A "Remember Me" Login System In ColdFusion
This may help: http://bugs.farcrycms.org/browse/FC-79 Its the session variables' option being selected in the CF Admin ... read »