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:
<cfscript>
function foobar() {
return getFunctionCalledName();
}
</cfscript>
<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:
component {
variables.x = 1;
variables.y = 2;
function init() {
return this;
}
function get() {
var name = getFunctionCalledName();
return variables[mid(name,4,len(name))];
}
function set(value) {
var name = getFunctionCalledName();
variables[mid(name,4,len(name))] = value;
}
this.getX = get;
this.getY = get;
this.setX = set;
this.setY = set;
}
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:
<cfset es = new es()>
<cfdump var="#es#">
<cfset es.setX("foo")>
<cfoutput>#es.getX()#</cfoutput>
So can folks think of any interesting uses for this?