Posted in ColdFusion | Posted on 08-18-2009 | 3,569 views
This didn't quite make the docs, nor do I think it was mentioned at CFUNITED, but one of the more interesting functions added to ColdFusion 9 is getFunctionCalledName. This returns the name of the calling function. Here is a somewhat useless example:
2function foobar() {
3 return getFunctionCalledName();
4}
5</cfscript>
6
7<cfoutput>#foobar()#</cfoutput>
This returns foobar, since the result of getFunctionCalledName is the name of the currently executing method. A slightly more sensible example, created by Elliott Sprehn, is the following:
2 variables.x = 1;
3 variables.y = 2;
4
5 function init() {
6 return this;
7 }
8
9 function get() {
10 var name = getFunctionCalledName();
11 return variables[mid(name,4,len(name))];
12 }
13
14 function set(value) {
15 var name = getFunctionCalledName();
16 variables[mid(name,4,len(name))] = value;
17 }
18
19 this.getX = get;
20 this.getY = get;
21 this.setX = set;
22 this.setY = set;
23}
Notice a few things in play here. He created one generic get and set function and then made copies of them for getX/getY and setX/setY. The generic functions work by using the getFunctionCalledName to figure out the real name of the function and update the appropriate value. As an example:
2<cfdump var="#es#">
3<cfset es.setX("foo")>
4<cfoutput>#es.getX()#</cfoutput>
So can folks think of any interesting uses for this?


error handling. Where you would be able to see the precise function that through you the error. Hmmm might
have to code something here.
Plus, the method names can still be dynamically generated, say, in the init() method! (I'm assuming this, haven't test it)
An Adobe forums user wanted to know how to find out what function called his function, or method. Back then with cf8 the only way was to force and catch an error to get the stack trace, and parse out the function name. A total hack, now I see they have provided a function to do that for you. Pretty cool.
sigh I need another coffee so that I am really awake
1) because it's own logic must be changed
2) because something changed in one or more callers
Ideally methods and/or object have only one potential reason for change.
Besides this caveat, this function allows for even more flexibility, which in my opinion is the number one advantage of CFML over other languages like Java. I wonder what people can come up with now that they know about this...
you can kind of do this in cf8, however it's extremely hackish. basically what you can do is create a java.lang.Execption and then parse out the information from the exceptions tagcontext. if you want to see it in the real world, have a look at the CFWheels source code for the $deprecated().
http://code.google.com/p/cfwheels/source/browse/tr...
We're using this technique to announce deprecated elements of the framework to developers and giving them a heads up that these elements could be removed in a future release.
No doubt Elliott is wicked smart (too bad he wasn't at CFUNITED this year - I hear he had an internship at Microsoft). His example is cool, but breaks if the target method is not stored in the variables scope.
We had a system setup where at the begging and end of functiosn we'd output a something like "Drawing start of table"...and then something similiar at the end the end. Of course the function only outputted when in debug mode.
At the end of the day you could throw a page into debug mode and easily find where in a template something was rendered. We tend to put lots of functions in our templates to better encapsulate them as they start to get really long.
This could help the developer find exactly what code is drawing a portion of the page. Hopefully it's return the CFC, CFM or whatever else might be useful.
[Add Comment] [Subscribe to Comments]