How To call a method dynamically within a method chain
I have been struggling with this issue for the majority of the day now, I am basically trying to figure out how to use a variable to specify a function name dynamically while using chaining in order to load and run a function from within a CFC.
Some background:
I designed a simple function which takes 2 arguments and has 1 hard-coded variable:
<cfargument name="cfcname" required="yes">
<cfargument name="args">
<cfset local.dir = "cfc">
if(isdefined("arguments.args")){
if(isstruct(arguments.args)){
REQUEST[cfcname] = new '#local.dir#.#cfcname#'(argumentcollection=arguments.args);
} else {
REQUEST[cfcname] = new '#local.dir#.#cfcname#'(arguments.args);
}
} else {
REQUEST[cfcname] = new '#local.dir#.#cfcname#'();
}
return REQUEST[cfcname];
I call the function "run" and then put it into the server scope after the function is defined, so that I can reference it anywhere on the server with the following syntax:
server.run('somefolder.somecfcfile').methodnamehere(argument1=thisvalue,argument2=thatvalue);
The end result of which is that it will:
look in the local.dir folder on the root of the server.
Drill down into the "somefolder" folder
open the "somecfcfile.cfc" file
run the "methodnamehere" function
and pass argument1 and argument 2 to the function... all in one line of code.
It works elegantly and has helped me to drastically minimize the code footprint for various bits of functionality needed in dozens or hundreds of files on a template-driven site for a client of mine.
HOWEVER, I have run into something of a brick wall with it: I CAN NOT seem to specify the function name using a variable; it was MY HOPE that something like this would work:
myfunctionname = "methodnamehere";
server.run('somefolder.somecfcfile').#myfunctionname#(argument1=thisvalue,argument2=thatvalue);
however, that does not work.
The CLOSEST I have gotten to getting this to work is something like this:
evaluate("server.run('somefolder.somecfcfile').#myfunctionname#(argument1=thisvalue,argument2=thatvalue)");
which actually works and does what I want it to do... but is there no form of "supported" syntax that can accomplish this without having to resort to evaluate??
