Using cfinvoke vs. createObject is in most cases a personal preference. However if you need to dynamically call a method whos name is inside a variable, cfinvoke works very well. Cfinvoke lets you do so without using eval() in much the same way using a structure lets you dynamically reference a variable whos name is in a variable (ie: #form[ "firstName_" & i ]#).
Below I have inserted a very simple example of how to do so. For a good example of why/when you can look inside the mach-ii framework invoker objects. All mach-ii objects are instantiated and their methods are called based on the configuration inside an XML file. Cfinvoke in this circumstance allows the framework to retrieve those config values into variables and dynamically create the named objects and invoke their named methods. I'm willing to bet ModelGlue and ColdSpring use this technique somewhere under the covers as well.
So back to my simple example of how:
<cfscript>
object = createObject("component","Person").init();
method = "setName";
args = structNew();
args.firstName = "John"
args.lastName = "Doe";
returnvar = "nameCopy";
</cfscript>
<cfinvoke
component="#object#"
method="#method#"
returnvariable="#returnvar#"
argumentcollection="#args#"></cfinvoke>
<cfdump var="#nameCopy#" label="Dump the return">
While you can easily dynamically pass in arguments to a method call without using cfinvoke its much more difficult (and ugly) to do so for the method and (depending on scope and code location) the return variable.
In sum, cfinvoke can at times be more flexible then createObject() and object.method() calls. Cfinvoke however cannot be called from within a cfscript block and tends to be more verbose which is why, unless I need the flexibility, I prefer createObject(). Hopefully this message was understandable.
-- Chris Stoner
On 3/9/06, Ung, Seng <[EMAIL PROTECTED]> wrote:
Hi:
from my test.cfc I do have the following function.
<cfcomponent >
<cffunction name="fnhello" returntype="string" access="private">
<cfset var x="hi">
<cfreturn x>
</cffunction>
</cfcomponent >
from my cfm, I called my test.cfc by doing the folloing code
<cfset o=createObject("component", "test")>
<cfset oFn=o.fnhello()>
Question number 1?
Once, I called or created an object named "o", Can I destroy the object by doing so? or by other methods?
<cfset o=null>
Question number 2?
Would you used "cfinvoke" or cfobject/createObject to call your cfc?
thanks
Seng
----------------------------------------------------------
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]
----------------------------------------------------------
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]
