On Monday, Mar 31, 2003, at 07:27 US/Pacific, Justin Balog wrote:
Quick question. Sorry if it has already been addressed. In Hals book
discovering CFCs he makes note of the encapsulation problem with the this
scope. He notes a work around, and also notes the work around's short
comings. However, there is no mention of a private scope? Is that
something new....or have I missed the boat all together? BRB - off to finish
Hal's book =) Its a good read.

There is no private scope in CF. Unqualified variables in a CFC are treated as protected scope.


        <cfcomponent displayname="foo.cfc">
                <cffunction name="foo">
                        <cfset this.publicVariable = 42>
                        <cfset protectedVariable = "Forty-Two">
                </cffunction>
        </cfcomponent>

        <cfcomponent displayname="bar.cfc" extends="foo">
                <cffunction name="bar" returntype="string">
                        <cfreturn protectedVariable>
                </cffunction>
        </cfcomponent>

test.cfm:
        <cfset obj = createObject("component","foo")>
        <cfset obj.foo()>
        <cfoutput>#obj.publicVariable#</cfoutput> <!--- 42 --->
        <cfset obj.publicVariable = 1234>
        <cfoutput>#obj.publicVariable#</cfoutput> <!--- 1234 --->

        <cfset obj = createObject("component","bar")>
        <cfset obj.foo()>
        <cfoutput>#obj.bar()#</cfoutput> <!--- Forty-Two --->
        <cfset obj.protectedVariable = "Zero">
        <cfoutput>#obj.bar()#</cfoutput> <!--- Forty-Two --->

Hope that clarifies?

A common idiom is to create a single protected variable called "instance" as a struct and store non-public data in that, so the above would become:

        <cfcomponent displayname="foo.cfc">
                <cfset instance = structNew()>
                <cffunction name="foo">
                        <cfset this.publicVariable = 42>
                        <cfset instance.protectedVariable = "Forty-Two">
                </cffunction>
        </cfcomponent>

        <cfcomponent displayname="bar.cfc" extends="foo">
                <cffunction name="bar" returntype="string">
                        <cfreturn instance.protectedVariable>
                </cffunction>
        </cfcomponent>

Sean A Corfield -- http://www.corfield.org/blog/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

----------------------------------------------------------
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