5

I am trying to work out the correct <cfscript> syntax for calling a dynamic method within ColdFusion 9. I have tried a number of variations and had a good search around.

<cfinvoke> is clearly the tag I want, sadly however I cannot use this within my pure cfscript component as it was implemented in ColdFusion 10.

i.e coldfusion 9 dynamically call method

I have tried the following within my CFC:

/** Validate the method name **/
var resources = getResources();
if (structKeyExists(variables.resources, name)) {
  variables.resourceActive[name] = true;
  var reflectionMethod = resources[name];
  var result = "#reflectionMethod.getMethodName()#"(argumentCollection = params);
}

Where the return value of reflectionMethod.getMethodName() is the method name I want to call. It is 100% returning the correct value (the name of the method) where that method is correctly defined and accessible,

My error is a syntax error on that line.

1
  • 1
    I use evaluate() to mimick <cfinvoke> in cfscript. It's not exactly the same, but haven't got any troubles with it so far.
    – jan
    Commented Sep 28, 2012 at 9:19

2 Answers 2

14

You don't want to get the method name, you want to get the actual method, eg something like:

function getMethod(string method){
    return variables[method];
}

The call that, thus:

theMethod = getMethod(variableHoldingMethodName);
result = theMethod();

Unfortunately one cannot simply do this:

result = getMethod(variableFoldingMethodName)();

Or:

result = myObject[variableFoldingMethodName]();

As the CF parser doesn't like the double-up of the parentheses or brackets.

The caveat with the method I suggested is that it pulls the method out of the CFC, so it will be running in the context of the calling code, not the CFC instance. Depending on the code in the method, this might or might not matter.

Another alternative is to inject a statically-named method INTO the object, eg:

dynamicName = "foo"; // for example
myObject.staticName = myObject[dynamicName];
result = myObject.staticName(); // is actually calling foo();
4
  • Thanks Adam this is helpful! Smells very "hacky" having to do all this. Could you possibly elaborate on the caveat you mentioned? The method(s) in question do reference/modify the variables scope which I fear may cause me issues?
    – AlexP
    Commented Sep 28, 2012 at 0:17
  • G'day Alex. My explanation is too verbose for a comment here, but I've written an article about it which covers it all: adamcameroncoldfusion.blogspot.co.uk/2012/09/… Commented Sep 28, 2012 at 7:57
  • @Adam Thank you very much for your answer and further time spent on the detailed explanation. Although it's not elegant ;-) it is now working correctly.
    – AlexP
    Commented Sep 28, 2012 at 11:24
  • 1
    Inject a statically named method into the object? Bloody genius. Commented Oct 29, 2014 at 17:03
0

Assuming the method is in your current (variables) scope, you could try:

var result = variables[reflectionMethod.getMethodName()](argumentCollection = params);
1
  • As Adam points out [bracket](paren) form doesn't work in ColdFusion 9. (It works in Railo, and I think recent versions of OpenBD, but the OP explicitly asks for CF9.) Commented Sep 28, 2012 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.