ColdFusion 9 Dynamic Method Call

后端 未结 2 517
名媛妹妹
名媛妹妹 2020-12-18 06:53

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

相关标签:
2条回答
  • 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();
    
    0 讨论(0)
  • 2020-12-18 07:33

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

    var result = variables[reflectionMethod.getMethodName()](argumentCollection = params);
    
    0 讨论(0)
提交回复
热议问题