Say I want to call a static function from a CFC from within a cfscript block. As far as I know, the only method is using createObject() which returns a reference to the CFC object.
Is this good practice? I think I remember reading that cfinvoke definitely instanciated objects smartly and would not instanciate a static CFC multiple times. Is this even true, and if so, is this still true when using createObject()?
CFOBJECT instantiates a component and creates a variable for it.
<cfobject type="component" name="path.to.my.CFC" name="myCFC" />
CFINVOKE can then reference the variable created by CFOBJECT, so that it doesn't have to re-create the object again.
<cfinvoke component="#myCFC#" method="foo" returnVariable="myValue" />
So you can call as many CFINVOKEs as you want on #myCFC#
without recreating the object.
However, CFINVOKE can also implicitly create the object for you if you don't also use CFOBJECT.
<cfinvoke component="path.to.my.CFC" method="foo" returnVariable="myValue" />
Calling multiple functions in this manner will recreate the object each time.
createObject()
works pretty much the same way. Either create the object first with a reference variable
<cfscript>
myCFC = createObject("component", "path.to.my.CFC");
myValue = myCFC.foo();
otherValue = myCFC.bar();
</cfscript>
or create the object with each function call.
<cfscript>
myValue = createObject("component", "path.to.my.CFC").foo();
otherValue = createObject("component", "path.to.my.CFC").bar();
</cfscript>
I prefer createObject()
since I've been using CFSCRIPT as much as possible. And I always create the object first if I'm going to call multiple functions from it.