We've covered how to write an Object.cfc, as well as how to create an instance of it. In order to understand how to use objects with Coldfusion, we have to first discuss what is and isn't inside an object.

What's in an object?

"That which we call a CFC, by any other name would encapsulate data or logic in a manner complementary to its task."

Or something like that. Anyway.

Let's look at that basic object again.

Object.cfc
view plain print about
1<cfcomponent output="false" name="Object" hint="Simple CF Object">
2
3    <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
4        <cfreturn this />
5    </cffunction>
6
7</cfcomponent>

The constructor returned "this". So what exactly is "this"?

object_test.cfm
view plain print about
1<cfset obj = createObject("component", "Object").init() />
2<cfdump var="#obj#" />
component Object
INIT
function init
Arguments:none
Return Type:Object
Roles:
Access:public
Output:false

So far, we haven't created any variables and we haven't set any values. But this doesn't mean that variables don't exist already.

The variables scope

When working with a normal CFM page, any variable you create without specifying a scope is placed into the variables scope.

Simple variable on a CFM page
view plain print about
1<cfset foo = 0 />
is the same as
view plain print about
1<cfset variables.foo = 0 />

Let's add a function that will return the variables scope from the object and see what's in there.

Object.cfc
view plain print about
1<cfcomponent output="false" name="Object" hint="Simple CF Object">
2
3    <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
4        <cfreturn this />
5    </cffunction>
6
7    <cffunction name="getVariablesScope" access="public" output="false" returntype="struct">
8        <cfreturn variables />
9    </cffunction>
10
11</cfcomponent>

object_test.cfm
view plain print about
1<cfset variables.foo = 0 />
2<cfset variables.obj = createObject("component", "Object").init() />
3<cfdump var="#variables.object.getVariablesScope()#" />
Variables scope of Object.cfc - struct
GETVARIABLESSCOPE
function getVariablesScope
Arguments:none
Return Type:struct
Roles:
Access:public
Output:false
INIT
function init
Arguments:none
Return Type:Object
Roles:
Access:public
Output:false
THIS
component_testing.oocf.Object
GETVARIABLESSCOPE
function getVariablesScope
Arguments:none
Return Type:struct
Roles:
Access:public
Output:false
INIT
function init
Arguments:none
Return Type:Object
Roles:
Access:public
Output:false

About the object's variables scope

  1. Just as with a normal CFM page, the variables scope is a struct
  2. Each function name is the name of a key in the struct
  3. "this" is in the variables scope of the component
  4. "this" contains an instance of the component itself

The most important thing to note here is that the variables scope of the CFM file and the variables scope of the CFC file do not know about each other.

Shared scopes

So what about server, application and session variables?

Let's dump the default session scope:

test.cfm
view plain print about
1<cfdump var="#session#" label="Session Scope (CFM)" />
Session Scope (CFM) - struct
cfid1234
cftoken56789012
sessionidFOO_1234_56789012
urltokenCFID=1234&CFTOKEN=56789012

Now we'll add a function to Object.cfc to return the session scope:

Object.cfc
view plain print about
1<cfcomponent name="Object.cfc" output="false" hint="A test Object">
2
3    <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
4        <cfreturn this />
5    </cffunction>
6
7    <cffunction name="getSessionScope" access="public" output="false" returntype="struct" hint="Returns session scope">
8        <cfreturn session />
9    </cffunction>
10
11    <cffunction name="getVariablesScope" access="public" output="false" returntype="struct" hint="Returns variables scope.">
12        <cfreturn variables />
13    </cffunction>
14
15</cfcomponent>

So can the object read the session scope?

test.cfm
view plain print about
1<cfset obj = createObject("component", "Object").init() />
2<cfdump var="#obj.getSessionScope()#" label="Session scope from inside Object.cfc" />
Session scope from inside Object.cfc - struct
cfid1234
cftoken56789012
sessionidFOO_1234_56789012
urltokenCFID=1234&CFTOKEN=56789012

Not only can an object read shared scope variables, an object can alter shared scope variables.

Object.cfc
view plain print about
1<cfcomponent name="Object.cfc" output="false" hint="A test Object">
2    <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
3        <cfreturn this />
4    </cffunction>
5    
6    <cffunction name="changeSessionScope" access="public" output="false" returntype="void" hint="Changes the session scope.">
7        <cfset session.rightNow = now() />
8    </cffunction>
9</cfcomponent>

test.cfm
view plain print about
1<cfset obj = createObject("component", "cfc.Object").init() />
2<cfdump var="#session#" label="Session scope" />
3<hr />
4<cfset obj.changeSessionScope() />
5<cfdump var="#session#" label="Session scope after change" />
Session scope - struct
cfid1234
cftoken56789012
sessionidFOO_1234_56789012
urltokenCFID=1234&CFTOKEN=56789012

Session scope after change - struct
cfid1234
cftoken56789012
rightnow{ts '2007-08-20 22:15:23'}
sessionidFOO_1234_56789012
urltokenCFID=1234&CFTOKEN=56789012

This means that unlike the variables scope, changes to server, application and session variables made from a CFM file can directly affect a CFC file and vice-versa.

Should an object access shared scope variables?

The majority of objects should not directly access shared scope variables.

Generally speaking, the only objects that should access shared scope variables are a Scope Facade or an Object Factory. I have an example of how my team used a Session Facade to solve a specific problem in another post, but I'll go further into these types of objects later in this Primer.

For most all other objects to use values from shared scope variables or any other external data, they have to get an injection.