i Know Kung Foo Consulting

Object Oriented Coldfusion : 2 : Anatomy of an Object.cfc

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
<cfcomponent output="false" name="Object" hint="Simple CF Object">

   <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
      <cfreturn this />
   </cffunction>

</cfcomponent>

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

object_test.cfm
<cfset obj = createObject("component", "Object").init() />
<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
<cfset foo = 0 />
is the same as
<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
<cfcomponent output="false" name="Object" hint="Simple CF Object">

   <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
      <cfreturn this />
   </cffunction>

   <cffunction name="getVariablesScope" access="public" output="false" returntype="struct">
      <cfreturn variables />
   </cffunction>

</cfcomponent>

object_test.cfm
<cfset variables.foo = 0 />
<cfset variables.obj = createObject("component", "Object").init() />
<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
<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
<cfcomponent name="Object.cfc" output="false" hint="A test Object">

   <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
      <cfreturn this />
   </cffunction>

   <cffunction name="getSessionScope" access="public" output="false" returntype="struct" hint="Returns session scope">
      <cfreturn session />
   </cffunction>

   <cffunction name="getVariablesScope" access="public" output="false" returntype="struct" hint="Returns variables scope.">
      <cfreturn variables />
   </cffunction>

</cfcomponent>

So can the object read the session scope?

test.cfm
<cfset obj = createObject("component", "Object").init() />
<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
<cfcomponent name="Object.cfc" output="false" hint="A test Object">
   <cffunction name="init" access="public" output="false" returntype="Object" hint="constructor">
      <cfreturn this />
   </cffunction>
   
   <cffunction name="changeSessionScope" access="public" output="false" returntype="void" hint="Changes the session scope.">
      <cfset session.rightNow = now() />
   </cffunction>
</cfcomponent>

test.cfm
<cfset obj = createObject("component", "cfc.Object").init() />
<cfdump var="#session#" label="Session scope" />
<hr />
<cfset obj.changeSessionScope() />
<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.


Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Gareth's Gravatar I'm still following along :). One thing, shouldn't it be <cfdump var="#obj#" /> instead of #object# in all of your examples? You are setting the variable name as variables.obj, not variables.object.
# Posted By Gareth | 8/31/07 9:07 AM
Adrian J. Moreno's Gravatar Yes and thanks for catching that.
# Posted By Adrian J. Moreno | 9/11/07 1:56 AM
Chris Rockett's Gravatar One slight correction to the section "About the object's variables scope"
4. "this" contains an instance of the component itself

The thing to note is that the this scope doesn't just contain an instance of the component but rather a POINTER the THIS instance of the component (as in the one you're calling the method on). It is not a separate instance from the one you are calling.
# Posted By Chris Rockett | 10/8/07 2:11 PM

Copyright © 2001 - 2008 Adrian J. Moreno and i Know Kung Foo Consulting
BlogCFC was created by Raymond Camden. This blog is running version 5.9.001.