Cool (and undocumented) ColdFusion 9 Feature

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> </code>

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; } </code>

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. 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.

Lafayette, LA https://www.raymondcamden.com

Comments