In ColdFusion cfc, what is the scope name for variables set outside the function?

In the ColdFusion / CFC component, I want some variables to be accessible to all contained functions, but be hidden or blocked from external scripts. What is the name of the cfc memory area? Are these "variables"? Is this available inside the function? Is it locked outside cfc?

(Examples in CF 8)

Call 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 recommendations are welcome. It has been a long time since I did this. Thanks in advance.

+7
coldfusion cfc
source share
4 answers

In the ColdFusion component, all public names are in the This scope, and all private names are in the Variables scope. Names can include both "normal" properties of variables and "UDF" methods. In the ColdFusion component, the This and Variables regions are instances and are not shared between instances.

Outside of the ColdFusion component, you can use any public names (the names that will be available in the component in the This ) using structure notation. You cannot access personal names.

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

Note that there is no such thing as a memory area. Areas of visibility, but not memory.

+14
source share

Yes, this is the default range.

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

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

Do not forget that output = "false", it cuts many CF spaces. I usually leave access = "public", as this is the default value.

If you want better documentation when others switch to your CFC, you might 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> 
+7
source share

I may have to answer my own question so that I can find it the next time I need it here in StackOverflow. I'm not sure, but I think it was. (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> 

Call Page:

 <cfset c = createObject("component","myComponent").init()> <cfoutput> <!--- Should output "development" ---> #c.getMode()# </cfoutput> 
+2
source share
 <cfcomponent> <cfset this.mode = "development"> </cfcomponent> 

Call Page:

 <cfset c = createObject("component","myComponent")> <cfoutput> #c.Mode# </cfoutput> 
+1
source share

All Articles