House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Confused about calling a function within a CFC

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Kear
05/30/2004 04:35 AM

I'm a bit confused about how to call a function inside a CFC by anther function.     I have a function called GETRANDID inside a cfc called utilities.cfc which creates a random id number.  It's access is 'Public" because I have to call it from several pages around the site.    Now I am building another function within the same CFC that does other jobs, but needs a RandomID.  I want to use the function GETRANDID to create the number, but how do I do it?   Every way I've tried gives me a very unhelpful "Page could not be displayed" error, which tells me I've got it wrong but not what the problem is. Can anyone tell me how, within a function called RENDERLINKS  I call the other function  called GETRANDID ? Cheers Mike Kear AFP Webworks Windsor, NSW, Australia http://afpwebworks.com

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
05/30/2004 08:53 AM

Just do <cfset result = getRandID()>. You call it like it was a UDF.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jim Davis
05/30/2004 10:48 PM

Ray already answered the question, but I thought I'd share my thinking on this as well. I've had many arguments about this view, but I find it helps tremendously to view CFCs as "persistable CF templates".  The more accepted view is to view them as objects, and this can be useful, but only if you already know objects or are attempting to replicate an object-based pattern or architecture. Looking at CFCs as CF templates is, I think, probably closer to the truth of how they were designed.  For me it also clears up some of the conceptual holes when you try to determine why the designers did certain things. For example: 1) In any CF template the "variables" scope is only viewable by that template - that's why it makes sense to me that the variables scope equates to the "private" scope for CFCs. 2) As Raymond said, the way you call a CFC method is exactly equivalent to way you would call any function in any CFC template. They really are the same - the terminology is different, but don't let that confuse you - if you know how to use functions in any CF page then you know how to use methods in CFCs. 3) In CFCs, just like in any CF template, processing occurs from top to bottom.  Any code outside of functions is run immediately.  When I think about it like this the "pseudo-constructor" (and lack of a real constructor) makes much more sense: it's just like any template - what's there runs like it would in any template. 4) Many common CFC questions can be answered easily when you start thinking like this as well.  CFCs use and access most scopes (CGI, Application, Server, Session, Cookie, etc) the same way as any template.  Although there are larger considerations to using them CFCs use shared scopes, custom tags and CFINCLUDEs the same way as any templates.  For example in a normal CF template you can CFINCLUDE a page of functions - those functions are then local to the template.  In a CFC you can CFINCLUDE a page of functions - those functions are then methods of the CFC. The big difference between normal CF templates and CFCs is that CFCs are persistable - in other words they can be run, but then stored in a shared scope and reused.  But even this can make more sense if you think about them as templates: basically a normal template runs, then stops and it's assets are destroyed.  CFCs are run, then stowed instead of being destroyed - their assets are still available indefinitely. You can then access them via their method calls (functions) but you can think of this as running a function on another CF page. The metaphor breaks down some when you get into thinking about CFCs as objects, but it still helps.  There've been may arguments about how CFCs aren't "OO enough" - and considered as objects they really aren't.  However considered as persistable CF templates with some object features it makes (at least to me) much more sense. CFCs just aren't objects - they are CF templates with some special capabilities (and limitations) and they act much more like CF templates for most purposes than like objects. I'm not sure if this helps or confuses you more, but I find thinking this way helps me immensely when conceptualizing how CFCs work. Jim Davis


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

September 09, 2010

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30