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.
8 response s so far ↓
1 Ben Nadel // Nov 25, 2008 at 10:12 AM
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 // Nov 25, 2008 at 10:17 AM
3 Sean Corfield // Nov 25, 2008 at 10:37 AM
4 Henry Ho // Nov 25, 2008 at 11:23 AM
5 Terrence Ryan // Nov 25, 2008 at 11:24 AM
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 // Nov 25, 2008 at 1:17 PM
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 // Dec 16, 2008 at 12:04 PM
8 Aaron Neff // Jan 6, 2010 at 2:29 AM
+1
Leave a Comment