ColdFusion 9 dynamic method call

I am trying to work out the correct <cfscript> syntax for invoking a dynamic method in ColdFusion 9. I tried several options and had a good search.

<cfinvoke> explicitly the tag that I want, but unfortunately I cannot use it in my pure cfscript component, as it was implemented in ColdFusion 10.

ie coldfusion 9 dynamic call method

In my CFC, I tried the following:

 /** 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); } 

If the return value of reflectionMethod.getMethodName() is the name of the method I want to call. This 100% returns the correct value (method name), where this method is correctly defined and available,

My mistake is the syntax error on this line.

+4
source share
2 answers

You do not want to get the method name, you want to get the actual method, for example, something like:

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

A call that way:

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

Unfortunately, you cannot just do this:

 result = getMethod(variableFoldingMethodName)(); 

Or:

 result = myObject[variableFoldingMethodName](); 

Because the CF analyzer does not like double brackets or parentheses.

The caveat with the proposed method is that it pulls the method from CFC, so it will work in the context of the calling code, and not from the CFC instance. Depending on the code in the method, this may or may not matter.

Another alternative is to introduce a statically called INTO method into an object, for example:

 dynamicName = "foo"; // for example myObject.staticName = myObject[dynamicName]; result = myObject.staticName(); // is actually calling foo(); 
+11
source

Assuming the method is in your current area (of variables), you can try:

 var result = variables[reflectionMethod.getMethodName()](argumentCollection = params); 
0
source

Source: https://habr.com/ru/post/1436711/


All Articles