On Tue, 28 Dec 2004 11:39:22 -0500, Jeff Small <[EMAIL PROTECTED]> wrote:
> <cfset init()>
> 
> <cffunction name="init" access="private" output="false" returntype="string"
> displayname="Initializes my object and creates the datasource variable">
>     <cfset variables.DSN = "myDataSourceName">
>     <cfreturn variables.DSN>
> </cffunction>
> 
> I know I could make it so that you have to pass the datasource variable name
> in to the function, but I figured, I can always just change it in one place
> in my CFC, and it's good to go...right?
> 
> That should suit my needs, right? If I want a variable that's available to
> all the functions in my CFC...right? Am I thinking the "right way"?
> 

Not quite.

1) The access type on an init() function should be public, or you'll
get an error saying the method doesn't exist when you invoke it in
your calling code..
2) If you really want to make the value of variables.DSN public (to
pass back to calling code), make the cfreturn its own getDSN method,
as shown below.
3) Is the <cfset init()> in the CFC itself? If so, it should not be.
It should be in your calling code.
4) You should be using the "hint" attribute rather than the
"displayname" attribute for your description.
5) One practice that has become a "best practice" of sorts is to
return "this" from your init method so that you can method chain, as
shown below. Granted, not everybody follows this practice, as it's
more personal preference, but it's just what I'm used to now.
6) Another practice that I use, but I know many don't, is to place my
CFC instance variables into a "instance" structure so that they're
neatly stored (i.e., variables.instance.DSN instead of just
variables.DSN).

It should be noted that you'll get slight variations from people based
on their preferences, but the code below generally covers best
practices that I've seen. Here's the cleaned up CFC and the calling
code is below it:

<cfcomponent output="false">
<cffunction name="init" access="public" output="false" returntype="CFCName"
hint="Initializes my object and creates the datasource variable">
    <cfset setDSN(dsName:"myDataSourceName")>
    <cfreturn this>
</cffunction>

<cffunction name="getDSN" access="public" output="false" returntype="string"
hint="Returns the name of the datasource">
    <cfset variables.DSN = "myDataSourceName">
    <cfreturn this>
</cffunction>

<cffunction name="setDSN" access="private" output="false" returntype="void"
hint="Sets the name of the datasource">
    <cfargument name="dsName" type="string" required="true" hint="The
datasource name to set">
    <cfset variables.DSN = arguments.dsName>
</cffunction>
</cfcomponent>

Calling code:

<cfset variables.objMyCFC = createObject("component", "path.to.my.CFC").init()>
<cfset variables.myDSN = variables.objMyCFC.getDSN()>

Here's a link to a community-driven document of best practices for
further reference:
http://www.dintenfass.com/cfcbestpractices/

Hope this helps?

Regards,
Dave.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Gold Sponsor - CFHosting.net
http://www.cfhosting.net

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188873
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to