Reputation: 31
I have put a cfc in application scope in applicationstart(). Is there a way I can access this in another cfc.
<cffunction name="method2" access="remote" returnType="Array" output="false">
<cfinvoke component="application.cfc1" method="method1" returnVariable="result" />
<cfreturn result>
</cffunction>
Thanks..
Upvotes: 1
Views: 155
Reputation: 263
There are a couple of different ways to do that. Henry's recommendation works. If you want to use cfinvoke
, you can do that too, but you need to understand how the tag works.
<cfinvoke component="application.cfc1" ... />
This syntax you used here where you're passing a string to the component attribute will look for the file "/application/cfc1.cfc" and attempt to invoke the method on that component. This is the rough equivalent of calling a static method from a Java class. It fails because you don't have a CFC file at that location.
If you already have a component instantiated then you need to pass the variable to the cfinvoke tag instead of a string literal. The simple change to make that work is to add the hashes around your variable name.
<cfinvoke component="#application.cfc1#" ... />
In general, this is how all ColdFusion tags work. If you put a value in an attribute without the hashes, you're giving ColdFusion a string literal value, not a variable. To provide a variable to the tag, you enclose your variable name in hashes.
This is surprisingly powerful at times and can also cause confusion if it's used in an unusual way. For example, I'm pretty sure this code would work, but should be avoided:
<cfset myvarname = "result" />
<cfinvoke returnVariable="#myvarname#" ... />
And then the result of your method call from the cfinvoke tag would be placed in the local variable named RESULT
, because that's the value of the myvarname variable that was supplied to the returnVariable attribute.
EDIT: I wanted to add clarification from the comments below. When cfinvoke
is called with a string-literal value for the component attribute (the first example above), Dan is correct, an instance of the component is created in order to call that method (there are no true static methods in ColdFusion). This might be undesirable (usually) if you're calling the method in a loop or otherwise calling it frequently, since you'd be creating a lot of whole objects that the ColdFusion server would then have to store in memory and soon garbage collect. Henry is also correct however, that if you pass an object reference with hashes (the second example above), the existing object is used instead of creating a new instance for each invoke, so you wouldn't have that performance problem.
Having said that, the primary reason I personally rarely use cfinvoke is because most of the time it's more keystrokes to do that, compared to calling the same method the way Henry recommended. Also, I'm not sure I've ever found a really compelling reason not to have an instantiated object, even if it's just a utility class that I load into my dependency injection framework (ColdSpring, LightWire, etc) so that my other objects can have access to it. There are a few edge cases though in which I've found having cfinvokeargument
useful.
Upvotes: 1