Hi Kevin,

Welcome ...

I'm not sure if this post would help you sort a few things out, but it might

http://aria-media.com/blog/index.cfm/2007/3/17/The-Joys-and-Challenges-of-Encapsulation

It seems you might be attempting to grapple with the whys and hows of
encapsulation in OO. At first, I know it can be very difficult to sort out.
Sometimes I think people don't even know that's what they are trying to work
out. Encapsulation is something you intentionally build into your code, for
several reasons that are fundamental to OO practice, and the proper use of
scoping in your CFCs is part of that.

When you employ the techniques of encapsulation, you build spaces where
other variables outside that space can't intrude or affect what happens
inside that space. A function, when constructed in an object oriented
manner, is one of those spaces, and var scoping is part of how you do that.

To answer your question, "should they always be scoped locally" ... there's
no always. If you need the variable encapsulated within the function, yes.
If you need it available to the rest of the CFC/object, no. Best practice if
you need it available within the rest of the object is use the variables
scope on it. So

cfset var thisVariableEncapsulatedInThisFunction = ...

cfset variables.thisVariableEncapsulatedInThisCFC = ...

cfset this.thisVariableNotEncapsulated = ...

Hope that helps you out a bit.

Nando

On 10/16/07, Kevin <[EMAIL PROTECTED]> wrote:
>
>
> I have been trying to teach myself cfc's and I am having trouble
> understanding this.
>
> I know you should var scope any variables that are available only to
> the instance in a call to a method.
>
> But should they always be scoped locally?
> eg.
>
> I have a function in a products.cfc
>
> <cffunction name="getdetail" access="public" returntype="query">
>     <cfargument name="item" default="" required="yes" type="numeric" />
>      <cfset qry_detail = "">
>     <cfquery name="qry_detail" datasource="#variables.datasource#">
>   SELECT TITLE, DESCRIPTION FROM mytable WHERE item = <cfqueryparam
> cfsqltype="cf_sql_integer" value="#arguments.item#" />
>    </cfquery>
> </cffunction>
>
> I have another function in the same cfc that uses the query to get a
> field if it has a value.
>
> <cffunction name="getdescription">
> <cfif len(qry_detail.DESCRIPTION) and qry_detail.DESCRIPTION neq 0>
>   <cfreturn qry_detail.DESCRIPTION>
> </cfif>
> </cffunction>
>
> And i just call it in the .cfm with #products.getdescription()#.
>
> Now I have been reading that if the var scope is not used on the
> query, a race condition can happen.
>
> But if I use
> <cfset var qry_detail = "">
> qry_detail.DESCRIPTION is not available to the getdescription function.
>
> Can a query used like this cause a race condition? Or should I redo
> the functions to contain the query only within var scope the cfc?
>
> Thanks ahead.
>
> >
>


-- 

Nando M. Breiter
The CarbonZero Project
CP 234
6934 Bioggio
Switzerland
+41 76 303 4477
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CFCDev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cfcdev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to