There is cfinvoke
as well as invoke()
in Adobe ColdFusion 10+ for script-based code.
Invoking dynamic method name (methodname from string)
-
11-04-2022 - |
Pergunta
If I have
<cfset methodName = "a">
<cfset object = createObject(...)>
How can I call the function named methodName
from the component object
. I would expect something along the lines of
<cfset object[methodName]()>
as would work in JS, but this doesn't seem to work. I know that I could use a cfinvoke
, however I don't think I need to delve into the reasons why I do not want to use that tag (using it as a temporary fix right now).
Solução
Outras dicas
You can set the unknown/dynamic method to an explicit key in the object and execute it from that key:
<cfset methodName = "a">
<cfset object = createObject(...)>
<cfset object.doMethod = object[methodName]>
<cfset object.doMethod()>
Still not very elegant, but it avoids <cfinvoke>
and evaluate()
.
It's klutzy but, you could always write the line of code to a .cfm file and then cfinclude it.
cfinvoke
or evaluate('object.#a#()')
(be sure that you can trust a)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow