In CFMX 6.1 you can use the variables scope to store 'private' data.

In 6.0 the variables scope will write variables to the calling page which is probably not what you want in most cases.

In 6.0 creating an un-scoped variable in a component makes it essentially 'private'*. I got into the habit of using instance as a structure to hold component data for 2 reasons. The first being that it makes it easy to return the instance data as a structure, and the second being that you _know_ that if you return instance.foo you're definitely returning the right data and not a locally scoped function variable.

In 6.1 this is less of an issue as you can always use the variables scope, but since I wasn't sure which version of CFMX Barry was using I decided to play it safe.

As Tim points out, using a structure to hold you instance data also makes it easier to searialize the data as the 'this' scope also contains the methods for the components.

* Just for the hair splitters out there, you can't really make anything in a component truly private. See the example below to see how to break encapsulation in a cfc. This obviously isn't a big issue, but it's worth knowing about because you can use it to do some interesting things.

foo.cfc
************************************
<cfcomponent>

<cfset init()>

  <cffunction name="init">
    <cfset instance = structNew()>
  </cffunction>

  <cffunction name="setfoo">
     <cfset instance.foo = "This data is private">
  </cffunction>

  <cffunction name="getfoo">
    <cfreturn instance.foo>
  </cffunction>
</cfcomponent>
*******************************************

testfoo.cfm
************************************************
<cfset oFoo = createObject('component','foo')>

<cffunction name="breakit">
  <cfset instance.foo = "Oops! there goes encapsulation.">
</cffunction>

<cfset oFoo.breakit = breakit>

<cfset oFoo.setFoo()>
<cfoutput>Foo value: #oFoo.getFoo()#<br></cfoutput>
<cfset oFoo.breakIt()>
<cfoutput>Foo value: #oFoo.getFoo()#</cfoutput>
*************************************************

Mark M wrote:

In fact, I don't remember the last time I wrote a component that used the 'this' scope because it encourages lazy shortcuts like directly modifying the data members of the component without going through a method.


Aaaaah!

I thought that it did that, I just wasn't sure!

That's some good stuff, I didn't know that was available!

I had just figured if you didn't assign it to the 'this' scope it would dissapapear into the variable ether. Obviously not.

Just figured all was public :oP which was fairly yuck, but i didn't see any way around it - glad to see there is a pseudo-private variable scope.

Cheers,

Mark

-----------------------------------
[EMAIL PROTECTED] ICQ: 3094740
Safe From Bees
[www.safefrombees.com]


---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]

MX Downunder AsiaPac DevCon - http://mxdu.com/





--- You are currently subscribed to cfaussie as: [EMAIL PROTECTED] To unsubscribe send a blank email to [EMAIL PROTECTED]

MX Downunder AsiaPac DevCon - http://mxdu.com/

Reply via email to