TerrenceRyan.com

I'm a 35 year old redhead geek from Philly.
I'm currently a Developer Evangelist for Adobe.
Also the author of Driving Technical Change

Cheap and Easy Dynamic Method Calling in CFScript

10 Comments

For reasons I cannot entirely explain, I was trying to call dynamic methods in a cfc in cfscript. (The cfc code was calling a method within itself) I tested a few syntactically wrong ways of getting it done. It the underlying fact that functions are another type of variable in ColdFusion hit me. I should be able to assign dynamic variable to a new variable, and call that as a function. It worked.

<cfscript>

function test(input){
   
return input;
}

functionName = "test";

args.input = "Yo!";

tempfunc = variables[functionName];

output = tempfunc(argumentCollection = args);

writeOutput(output);

</cfscript>

I feel like someone must have done this before, but could find nothing on it. So I figured I would blog it.

10 responses so far ↓

  • 1 Ben Nadel

    Looks good. I wish that we could do something like this:

    variables[ functionName ]();

    This works at a "functional" level (as it is what you are essentially doing); but, this fails to compile due to parsing issues.
  • 2 Terrence Ryan

    I agree, Ben, as that method would also work with method calls on other objects too.
  • 3 Sean Corfield

    I think you'll find that if you do this with CFC methods, the called function doesn't correctly bind to VARIABLES or THIS scope...
  • 4 Henry Ho

    @Sean, so this method doesn't really work for calling CFC methods?
  • 5 Terrence Ryan

    Do you mean intra-cfc or external to that CFC?

    I know it doesn't work externally. but it does work intra-cfc. I assume as the variables and this scope are same for the dynamic call.
  • 6 Mark Mandel

    @Terrence, yeah, you are fine intra-cfc. I do this all the time when I don't want to escape out of a cfscript block to use cfinvoke.

    Either that or when I pass a function to a function, as otherwise, for some reason cf throws a wobbly if you try and call arguments.foo(argumentCollection=args);
  • 7 Joshua Curtiss

    Perfect. This is perfect timing. I needed this for a Transfer decorator using onMissingMethod(), and it works great. Thanks!!!
  • 8 Aaron Neff

    "variables[functionName]();"

    +1
  • 9 delijah

    Does not work on persistent objects :(
  • 10 Chris Weller

    It appears I'm late to the party on this one, but I have several methods in a CFC for saving form data. I was trying to create a single action page to call these save functions, but do so dynamically similar to what you've done here. I just used cfinvoke method="#functionName#". Is there a drawback to doing it this way with cfinvoke? Thanks for the great example!

Leave a Comment









Categories

Monthly Archives

Tag Cloud

coldfusion web development flex coldfusion builder appearances squidhead coldfusion builder extensions higher ed flash builder air mobile android adobe apptacular html5 driving technical change running a coldfusion shop adobemax06 movable type flash catalyst flash blackberry adobemax07 adobemax08 hero finicky css adobemax09 holy crap i’m a mobile developer centaur basecamp cfc unfuddle motorola metablog irrational characters ios git evangelism devices code reviews ant wharton subversion security phonegap philly philadelphia multidevice knowledge@wharton jobs browserlab adobemax10 adobe tv unfuddlecfc svnauth.cfc semantic html semantic html responsive web design qnx nlb linux jquery mobile java it github flexorg fireworks edge eclipse dreamweaver apps apple adobemax11