ColdFusion 9 Dynamic Method Call

妖精的绣舞 提交于 2019-11-29 07:32:29

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();

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

var result = variables[reflectionMethod.getMethodName()](argumentCollection = params);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!