Object Oriented Coldfusion : 2 : Anatomy of an Object.cfc
Posted on August 23, 2007
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 |
|
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 |
| ||||||||||||||||||||||||||||||||
INIT |
| ||||||||||||||||||||||||||||||||
THIS |
|
About the object's _variables_ scope
- Just as with a normal CFM page, the variables scope is a struct
- Each function name is the name of a key in the struct
this
is in the variables scope of the componentthis
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 | |
---|---|
cfid | 1234 |
cftoken | 56789012 |
sessionid | FOO_1234_56789012 |
urltoken | CFID=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 | |
---|---|
cfid | 1234 |
cftoken | 56789012 |
sessionid | FOO_1234_56789012 |
urltoken | CFID=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 | |
---|---|
cfid | 1234 |
cftoken | 56789012 |
sessionid | FOO_1234_56789012 |
urltoken | CFID=1234&CFTOKEN=56789012 |
Session scope after change - struct | |
---|---|
cfid | 1234 |
cftoken | 56789012 |
rightnow | {ts ‘2007-08-20 22:15:23’} |
sessionid | FOO_1234_56789012 |
urltoken | CFID=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 shouldaccess shared scope variables are a Scope Facade or an Object Factory. In another post, I have an example of how my team used a Session Facade to solve a specific problem, 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.
Adrian J. Moreno
Adrian is a CTO and solution architect specializing in software modernization. More information