Since you asked, a few other thoughts:

1) A lot of people have taken to creating an "init" method in their CFCs --
this is parallel to the way CF handles calling the constructor of java
objects, so it makes code consistent.  The init method can then take
whatever arguments you want.  Personally, I like to return "this" in my init
methods, which lets me do:

foo = createObject("component","foo").init(arg,arg2);

(as an aside, that is the only time I ever use "this" in my CFCs because I
hate having "private" data in the "public" scope)

I always put an init method in my components, even if it does nothing at
all -- that way I just know that I should ALWAYS run init() when
instantiating -- I find that easier and more consistent, and it prevents me
from having to worry about whether or not a given component needs to have
init() run (and makes it easier to add an init method later without needing
to change a lot of code).


2) Another common practice is to create a "virtual scope" in the unnamed
scope.  I call mine "instance" (I've also seen it called "private" and
"my").  Basically, I just do this in the "pseudo-constructor":

<cfset instance = structNew()>

(I sometimes use CFPARAM, which is better if you are extending another
component -- but, that's a nuance)

I then use "instance." as the prefix for all instance variables -- this
makes code more readable, IMO, and prevents any name collisions with
arguments or local (to a method) variables.  Thus, I can do things like:

<cffunction name="setFoo"....>
<cfargument name="foo">
<cfset instance.foo = arguments.foo>
</cffunction>

Some have told me I should never use the same names for instance vars and
arguments like that, but I find it to be nice to refer to something by the
same name in different scopes.

I've also seen some people use an "_" to prefix all "private" instance
variables.

(I am using quotes around "private" because in CFCs data is never really
private at all -- more like "protected" in java, and not even actually like
that).


3) Be sure to put OUTPUT="false" in your CFFUNCTION and CFCOMPONENT tags --
otherwise you get white space in your calling page.


4) This is worth repeating even though it has been covered ad nauseum on
this list: ALWAYS ALWAYS ALWAYS use the "var" keyword for ALL local (to a
method) variables.  This includes "i" in loops, names of queries, etc. --
you will never regret doing it and could end up pulling your hair out if you
don't.





> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of cf_nut
> Sent: Friday, July 25, 2003 9:42 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [CFCDev] CFC basics
>
>
> --- "Paul Johnston" <[EMAIL PROTECTED]> wrote:
> > 3) There is no such thing as a constructor in CFC's.  To "hack"
> >  (albeit a
> > known and recommended hack) then you insert code outside
> > <cffunction> tags,
> > but inside the <cfcomponent> tags.  This code gets run each time
> > the CFC is
> > invoked.
>
> Almost right. The code gets run at the time the component is
> instantiated, not every time it's invoked. Hence it's common (but
> probably incorrect) name, the "constructor" code.
>
> Andy.

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

Reply via email to