Cool (and undocumented) ColdFusion 9 Feature

This post is more than 2 years old.

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>


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?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA