Title: RE: [CFCDev] Calling a cfc method whose name is in a variable
Thanks everyone for these various approaches, I'll check them out. Definitely some issues here I wasn't aware of.
 
Adam, I hate to sound more OO-gnorant that I already have, but why is cfinvoke so evil? Is it cfexecute-like internally?
 
Dave Merrill
 

Eiither:
<cfset method = myObj[method] />
<cfset method() />

In CFMX61, anyway (dunno about CFMX7: but no reason to believe it's changed), you might find you have problems with this if myObj[method] contains calls to other methods within the CFC that are private.  Indeed, I've just tested this and I can't get it to work even if the embedded method call is public:

<!--- c.cfc --->
<cfcomponent>
        <cffunction name="f">
                <cfreturn p()>
        </cffunction>

        <cffunction name="p">
                <cfreturn "private!">
        </cffunction>
</cfcomponent>

<!--- caller.cfm --->

<cfset myObj = createObject("component", "c")>
<cfset method = "f">
<cfset method = myObj[method]>
<cfset method()>

This errors saying it can't find variable p.

(I'm sure it was only PRIVATE methods that had this issue, last time I tested, but samesame with public ones in this test...?)

So, anyway, to get around this issue, I do this instead:

<!--- caller.cfm --->

<cfset o = createObject("component", "c")>
<cfset dynamic = "f">
<cfset o.temp = o[dynamic]>
<cfoutput>
#o.temp()#
</cfoutput>

Much the same as your suggestion, except I poke the "non-dynamically-named method" back into the CFC.

Seems to work.  A bit grim, but.

I can't bring myself to use <cfinvoke>, but I'd recommend it to others if asked.
--
Adam

---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected]

Reply via email to