Hello CF coders,
I am new to coldfusion and it seems very nice to use. I used to be a PHP coder, but then switch last week because i like some of the things that CF has to offer. I am currently using Coldfusion 9 with Dreamweaver CS5. The one question i had, was how to use and reference a CFFunction with a CFM document. I have read some stuff, but im still lost. I know im not supposed to do this, but can someone just show me how to implement a CFFunction? So lets say this is _component.cfc

<cfcomponent>
    <cffunction name="myFunction" access="public" returntype="string">
        <cfargument name="myArgument" type="string" required="yes">
        <cfset myResult="foo">
        <cfreturn myResult>
    </cffunction>
</cfcomponent>

Now what im lost is, if i have another page called _getcomponent.cfm, how do i call the function? Also, if i have an argument, does that mean that my function will look like this

<cfoutput>#myFunction('myArgument')#</cfoutput>

Im sorry, but again this is all new to me. Thanks for anyhelp that people my provide.

Fobos

Recommended Answers

All 3 Replies

Hi,

Try this for your _getcomponent.cfm

<cfobject name="myObj" component="mywebapp.cfc.path" >

<cfinvoke component="#myObj#" method="myFunction" myArgument="value or variable">

You can do this in cfscript but for now this is probably the easiest way to learn..

b

It can be confusing at first. Keep in mind there's different ways you can call functions, and reasons why you'd use 1 way versus another way. But let's ignore that for now and get back to your question.

In order to use the function, you have to create an instance of the component first. The simplest way to do that is using CFINVOKE. The "component" value is normally a dot-notation path to your component. But if both your files are in the same directory, you can just use the component name: ie "_component" without the .cfc extension. Use "returnVariable" to capture the results of your function.

<cfinvoke component="_component" 
          method="myFunction" 
          myArgument="some value here"
          returnVariable="result" >

BUT ... you could also call it like brettskiii showed in the previous post. You could also call it a 3rd way (my personal preference). I find it less bulky:

<!--- create an instance of the component --->
<cfset myObj = createObject("component", "_component")>
<!--- then call the function --->
<cfset result = myObj.myFunction( "some value here ")>

For uber-clarity you could use the argument names when you call the function. But it's not strictly needed here.

ie <cfset result = myObj.myFunction( myArgument="some value here ")>

Anyway, those are the 3 main ways you can call a function within a component

Wow thanks alot guys. This really helps to me to have a better understanding. Again thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.