Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / May 2008



Tip: Looking for answers? Try searching our database.

CFC question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SteveLogan1029 - 08 May 2008 19:54 GMT
How do I call a function from another function in the same CFC?

For example, let's say that I have a CFC called:  cart.cfc

One of the functions in the cfc is:  getCartSubTotal, which takes one argument
- the cart ID

Now in the same cart.cfc, I want to have a function called getShipping.  

I'm thinking I should be able to pass it the same cart ID and have it get the
cart sub total from the function getCartSubTotal.  But how do I call that other
function from the new function?
Dan Bracuk - 08 May 2008 20:36 GMT
cfinvoke argumentcollection=arguments

is a handy piece of code.
Azadi - 09 May 2008 04:16 GMT
inside same cfc you can call a function like
<cfset myvar = functionname(argument1, argument2, ..., argumentN)>
check out the docs for various ways to pass arguments to a function -
the above example assumes you pass args to the function in same order
they appear in the cffunction tag.

in your case, you can have code like below in your getShipping function:

<cffunction name="getShipping" access="public">
<cfargument name="cartid" type="numeric" required="yes">
<cfset var cartSubTotal = getCartSubTotal(arguments.cartid)>
....
</cffunction>

hth

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Eivind - 09 May 2008 08:55 GMT
SteveLogan1029 skreiv:
> How do I call a function from another function in the same CFC?
>
[quoted text clipped - 4 lines]
>
>  Now in the same cart.cfc, I want to have a function called getShipping.  

You've got three choises, basically. (there are others, but they're silly)

1) simply call the second method with the desired arguments
<cfset shipping = getShipping(cartID)>

2) Store the cartID in the component, and have getShipping use it from
there:
<cfset variables.cartID = cartID>
<cfset shipping = getShipping()>
(the inside of getshipping can then refer to variables.cartid)

3) Use cfinvoke, which is basically just a different syntax for the same
thing as 1)

    Eivind
GArlington - 09 May 2008 09:32 GMT
> SteveLogan1029 skreiv:
>
[quoted text clipped - 17 lines]
> <cfset shipping = getShipping()>
> (the inside of getshipping can then refer to variables.cartid)

" 3) Use cfinvoke, which is basically just a different syntax for the
same thing as 1)" => Not quite true, cfinvoke is the same (different
syntax) as CreateObject, so it will create another instance of the cfc
and call the method from that instance = waisted resources and you
will NOT be able to use component (instance) variables, because they
will be different...

>         Eivind
GArlington - 09 May 2008 09:39 GMT
> SteveLogan1029 skreiv:
>
[quoted text clipped - 22 lines]
>
>         Eivind

For best results in resource use and flexibility use method 1) => you
will be able to use ONE instance of the cfc (in Application scope) for
ALL sessions if you code all functions to use function-local [<cfset
VAR newVar = ... />] variables.
Method 2) will make you use one instance of the object per session =>
if you have a lot of active sessions you will have to have as many cfc
instances.
Eivind - 09 May 2008 10:55 GMT
GArlington skreiv:

>> 1) simply call the second method with the desired arguments
>> <cfset shipping = getShipping(cartID)>
[quoted text clipped - 7 lines]
>> 3) Use cfinvoke, which is basically just a different syntax for the same
>> thing as 1)

> For best results in resource use and flexibility use method 1) => you
> will be able to use ONE instance of the cfc (in Application scope) for
[quoted text clipped - 3 lines]
> if you have a lot of active sessions you will have to have as many cfc
> instances.

True dat. It really depends on how the application is structured. I
agree there is no reason to use storage in the component if that isn't
already the case.

I was mostly thinking, since this CFC is named "cart", if there was
already one such cart-object in each session containing the
cart-content, then it migth already have the cartID stored internally,
in which case getshipping() could simply access that rather than getting
it passed in.

I just didn't feel like writing a lecture on OO design.

So I'm with you: Do it like GArlington suggests unless you know what
you're doing and have your reasons for choosing differently. (in which
case you probably wouldn't have needed to ask her anyway)

    Eivind
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.