In a Coldfusion cfc, what is the scope name for variables set outside of a function?
Asked Answered
C

4

7

In a Coldfusion component / CFC, I want to properly scope some variables to be available for all contained functions, but to be hidden or blocked from outside scripts. What is the name of the cfc's memory scope? Is it 'variables'? Is that available inside a contained function? Is it blocked from outside the cfc?

(Examples in CF 8)

Calling page:

<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
    #util.myFunction()#
</cfoutput>

myUtils.cfc:

<cfcomponent>
<!--- Need to set some cfc global vars here --->

    <cffunction name="init" access="public">
        <cfargument name="settings" type="struct" required="no">
        <!--- I need to merge arguments.settings to the cfc global vars here --->
        <cfreturn this>
    </cffunction>

    <cffunction name="myFunction" access="public">
        <cfset var result = "">
        <!--- I need to access the cfc global vars here for init settings --->
        <cfreturn result>
    </cffunction>
</cfcomponent>

Additional best practice suggestions are welcomed. It's been quite a while since I've done this. Thanks in advance.

Centuple answered 17/12, 2009 at 19:49 Comment(0)
H
14

Within a ColdFusion component, all public names are in the This scope and all private names are in the Variables scope. Names may include both "normal" variable properties as well as "UDF" methods. Within a ColdFusion component, the This and Variables scopes are per-instance and are not shared between instances.

Outside a ColdFusion component, you may use any public names (names that would be available within the component in the This scope) using the struct notation. You may not access any private names.

The default scope is always Variables - within a component, outside of a component, within a UDF, within a component method, etc.

Note that there is no such thing as a "memory" scope. There are named scopes, but not memory scopes.

Hughs answered 17/12, 2009 at 20:23 Comment(0)
C
7

Yes, it is the default, variables scope.

<cfcomponent output="false">
    <cfset variables.mode = "development">

    <cffunction name="getMode" output="false">
        <cfreturn variables.mode>
    </cffunction>
</cfcomponent>

It is a good idea to scope all your variables in cffunctions in CFC.

Don't forget output="false", it will cut down a lot of whitespace CF generates. I usually will leave out access="public" since that's the default.

If you want better documentation when others browse to your CFC, you may even consider using

<cfcomponent output="false">
    <cfproperty name="mode" type="string" default="development" hint="doc...">

    <cfset variables.mode = "development">

    <cffunction name="getMode" output="false">
        <cfreturn variables.mode>
    </cffunction>
</cfcomponent>
Cryptoclastic answered 17/12, 2009 at 20:18 Comment(0)
C
3

I may have to answer my own question so that I can find it next time I need it here on StackOverflow. I'm not positive, but I think this is how it's done. (As always, corrections and suggestions are welcome!)

<cfcomponent>
    <!--- Server Mode: production | development - used for differing paths and such --->
    <cfset variables.mode = "development">

    <cffunction name="init" access="public">
        <cfargument name="settings" type="struct" required="no">
        <cfset StructAppend(variables, arguments.settings)>
        <cfreturn this>
    </cffunction>

    <cffunction name="getMode" access="public">
        <cfreturn variables.mode>
    </cffunction>

</cfcomponent>

Calling page:

<cfset c = createObject("component","myComponent").init()>
<cfoutput>
    <!--- Should output "development" --->
    #c.getMode()#
</cfoutput>
Centuple answered 17/12, 2009 at 20:9 Comment(1)
I would list out the supported settings one by one with cfargument's, document what they are with hint="", and then the caller can use init(ArgumentCollection="#setting_struct#") if there're a lot of arguments. Better documentation that way.Cryptoclastic
K
1
<cfcomponent>
<cfset this.mode = "development">
</cfcomponent>

Calling page:

<cfset c = createObject("component","myComponent")>
<cfoutput>
#c.Mode#
</cfoutput>
Knighten answered 19/12, 2009 at 19:30 Comment(1)
@cf_PhillipSenn - I believe they were asking about scopes that are not visible outside the cfc.Interchange

© 2022 - 2024 — McMap. All rights reserved.