ColdFusion 9 Dynamic Method Call
Asked Answered
W

2

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.

Wilen answered 27/9, 2012 at 23:7 Comment(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.Ego
H
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();
Hammering answered 27/9, 2012 at 23:30 Comment(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?Wilen
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/…Hammering
@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.Wilen
Inject a statically named method into the object? Bloody genius.Wage
R
0

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

var result = variables[reflectionMethod.getMethodName()](argumentCollection = params);
Riesling answered 27/9, 2012 at 23:13 Comment(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.)Reichsmark

© 2022 - 2024 — McMap. All rights reserved.