CFInvoke vs. CreateObject

Friday, September 26th, 2008

Most programming languages have what are called static or class methods.  The other kind of method available is instance or object methods.  The difference between the two is that the former acts independently of any data members within the object.  Thus it can be called in passing without instantiating an entire object in order to use a small chunk of code.

Apparently the Coldfusion developers have never heard of these differences.  A tag called CFInvoke exists which would lead one to believe that this works like a static method call.  The first volume of the official Coldfusion text book even hints at this by using it in this way.  It even say, “… although rather than used, CFCs are said to be invoked.”

Well, not exactly.  If you use CFInvoke on an uninstantiated object (by using the component path), it will instantiate it for you.  Everytime. So instead of it being a memory saver, it wastes memory.

The proper use of CFInvoke is if you do not know what function will be called ahead of time and don’t want to have to have a switch statement for each potential function call.