Posted At : August 22, 2007 11:27 PM | Posted By : Adrian J. Moreno
Related Categories:
OOP, ColdFusion, Primers
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.
The constructor returned "this". So what exactly is "this"?
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.
Let's add a function that will return the variables scope from the object and see what's in there.
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 component
"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:
Now we'll add a function to Object.cfc to return the session scope:
So can the object read the session scope?
Not only can an object read shared scope variables, an object can alter shared scope variables.
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.
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.
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.
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.