INSTANCE scope is never an official scope, but it has been adapted by many CFer's (including myself) for writing CFC.
<cfset variables.instance = structNew()>
Common usages include:
- dump() private variables for debugging
- getMemento() / setMemento(struct)
- clone()
However, CF9 brings lots of much needed <cfproperty> functionality:
- <cfdump> will now dump all properties
- implicit getters and setters (can be turn off optionally)
- validation on setter (optional)
- metadata for the almighty ORM with Hibernate
Unfortunately, all the properties are stored in VARIABLES scope by default, and it can’t be changed. However, since <cfproperty> has become so important and powerful in CF9, should we abandon the INSTANCE scope convention?
What do you think?
Henry,
ReplyDeleteyou will find that making use of properties and also the LOCAL scope is a more refined process and will allow you to forego the use of the instance scope many of use have come to use.
I agree - I think the combination of the new local scope coupled with the ability to use var statements anywhere will help secure variables from leaking. The idea of an "instance" scope was to prevent memory leaking in the first place and I think many of our problems there will go away in 9.
ReplyDeleteI disagree. The purpose of instance scope was not for memory leak but for distinguishing between an object's instance data and methods. By using cfproperty to put all variables in the variables scope we still have a problem that we cannot get clearly the state of an object or replace the state of an object without some trickery because all methods are in the variables scope.
ReplyDeleteHow would you implement the state pattern then? You would have to loop through an incoming memento and try not to override functions.
So in my opinion instance is relative, however, cfproperty has that limitation that will only add to variables scope. I remember asking this in the beta forums, why not have the ability to choose where the cfproperty variables are defined in.
I agree with Pio. I'm disappointed that properties in CF9 get automatically assigned to the variables scope. Having an separate struct with instance data (an not its methods) has been very useful to me.
ReplyDeleteThere's a handy function on Adobe Cookbooks which implements a getMemento() method by looping over the component's properties, and returning the values as a struct...
ReplyDelete