tags:

views:

212

answers:

3

How do I invoke a static method on a CFC without using cfinvoke? I know that I can do this:

<cfinvoke component="MyComponent" method="myStaticMethod' arg1="blah" returnvariable=myReturnVar>

I would like to be able to invoke this method the same way I would a UDF:

<cfset myReturnVar = MyComponent.myStaticMethod(blah)>

This, however, does not work. Is there syntax that I am messing up or is this just not possible?

+4  A: 

not possible, since there's no "static method" in ColdFusion.

The <cfinvoke> line in your question is the same as:

myReturnVar = CreateObject("component", "MyComponent").myStaticMethod(arg1="blah");
Henry
Thanks, I thought that might be the case but wasn't sure.
derivation
One thing to keep in mind is that if you were using CreateObject to make a JAVA object then you would do the same thing as here....you have to make a static reference to the class (by NOT calling init)....before calling the static method.
ryber
Also, you should prefer this over <cfinvoke> because it's more efficient for CF internally even though that may seem counter intuitive.
Bialecki
@Bialecki really? why is it more efficient?
Henry
@Henry somewhere in the CF documentation they mention that creating a component and calling a method on it is more efficient than <cfinvoke>. Very odd, I know, but it was a bug in CF 7, don't know if it was fixed in 8 or 9. I'll poke around for a link to it.
Bialecki
If you find it, I would be interested in reading about it too. The only performance recommendation I was aware of regarded reuse. ie It is more efficient to create once, and reuse an object multiple times, rather than creating a new object each time ie Calling <cfinvoke component="MyComponent" method="..."> over and over.
Leigh
+3  A: 

You need to create the object first.

<cfset MyComponent = createObject("component","MyComponent") />
<cfset myReturnVar = MyComponent.myMethod(blah) />
Al Everett
This is the same as what Henry wrote, not sure why it was downvoted
ryber