> It's interesting how ColdFusion 5.0 seems to allow these 
> functions to be saved in a variable scope by referencing 
> the functions as if they were variables [scope.func=func]... 
> rather than by referencing them as if they were functions 
> [scope.func=func() or scope.func()=func()]...

Actually, given the behavior of functions in other languages such as
JavaScript, this is the behavior I'd expect. For example, if you wanted to
assign a function to the window error handler, you might have something like
this:

function myerrorhandler(msg, url, lno) {
        ...
}

window.onerror = myerrorhandler;

Of course, JavaScript is an object-oriented language, while CF isn't.

> I haven't tested yet what happens when you have a local variable 
> named "mylocal" and a local function named "mylocal"... then 
> set server.mylocal=mylocal to see if it saves the variable in 
> server scope or the function in server scope...  

I haven't tested this, but as a wagering man, I'll bet that whichever one
you declare last will be used. That's the standard behavior in CF for
duplicate variables within the same scope.

> Does anyone know if this "trick" is an official behavior of CF5.0 
> or something along the lines of <CFSET "server.#myDynVar#" = value> 
> and something that is not recommended?

If a user-defined function can be treated as a variable, there's no reason
why this behavior wouldn't be considered "official" since it would follow
the normal behavior of variables.

As for the CFSET example you provide, I used to recommend against that, and
still personally prefer using SetVariable, but it's mentioned in the CF
documentation, so I guess it's official in that sense. (Even though CF
documentation tends not to be authoritative in any meaningful sense.)

Finally, however, I don't know that it's a good idea to "globalize"
functions by storing them in variables so that you can use them within
custom tags. After all, when you're building a custom tag, you want it to be
essentially self-contained to the greatest extent possible - you pass it
parameters, and it returns values. The placement of user-defined functions
within persistent memory variables violates this model, although it does
make it easier to use UDFs within custom tags. Instead, I'd probably stick
with an explicit inclusion of UDF libraries using CFINCLUDE within custom
tags, or simply physically including the necessary code within the custom
tag. I'd probably go with the first of those two, unless building something
for widespread distribution.

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
voice: (202) 797-5496
fax: (202) 797-5444

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to