Makes sense. Thanks for the explanation, Ian! Rick
> -----Original Message----- > From: Ian Skinner [mailto:[EMAIL PROTECTED] > Sent: Friday, June 20, 2008 11:25 AM > To: CF-Talk > Subject: Re: Waaahhhh! Why won't my CFC work! > > Rick Faircloth wrote: > > Read inline. > > > Thanks for the info, Matt. > > > > I think I've got a handle on those methods. > > > > One thing I still haven't been able to get to work > > is setting up an "init" function for my db in a cfc. > > > > Can you explain how that work and show me an example > > of the code I need to put in a cfc. > > > > I tried this that someone gave me: > > > > <cffunction name="init"> > > <cfargument name="dsn"> > > <cfset variables.dsn = arguments.dsn /> > > <cfreturn this> > > </cffunction> > > > > But that doesn't make sense to me. My dsn is set > > in application.cfm like so: cfset application.dsn = "myDB". > > So why, "variables.dsn" above? > > > The 'variables' scope inside of a CFC is the private, global scope of > the component. Data in this scope is accessible to any function or code > inside the CFC but not by anything outside the CFC, i.e. the calling > page. The 'this' scope is also global, but it is also public meaning > that it can be read and modified by code outside of the CFC. Many > people consider this to be poor encapelization of data and recommend > against using it. They also recommended that a component should not use > data that is not contained with-in it. So it is considered poor > practice to have a component rely on something like the application > scope. So most of us would pass the data in, often with this defacto > constructor function named 'init' so that it is contained within the > object and does not rely on outside data. > > You can have trouble with things like the application scope when you > move CFC to different places in the file system so that the may not be > under the 'application' has ColdFusion sees it and thus does not have > access to that application scope anymore. > > > I just don't understand what the code is doing. > > Why is variables.dsn being set to be the value of > > arguments.dsn? And what exactly does "cfreturn this" do? > > > Arguments is the scope of the data passed in with the function call. By > transferring it to the variables scope allows for this data to pesist > for the life of the component, beyond the end of this function call. > The return this returns 'this' instigated component to the calling code > so that it can be stored and used later. > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to date Get the Free Trial http://ad.doubleclick.net/clk;203748912;27390454;j Archive: http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:307844 Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4